

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>scitools.misc &mdash; SciTools 0.8.3 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.8.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="SciTools 0.8.3 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for scitools.misc</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">A collection of Python utilities originally developed for the</span>
<span class="sd">&quot;Python for Computational Science&quot; book.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">re</span><span class="o">,</span> <span class="nn">getopt</span><span class="o">,</span> <span class="nn">math</span><span class="o">,</span> <span class="nn">threading</span><span class="o">,</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">commands</span>
<span class="kn">from</span> <span class="nn">errorcheck</span> <span class="kn">import</span> <span class="n">right_type</span>
<span class="kn">from</span> <span class="nn">scitools.StringFunction</span> <span class="kn">import</span> <span class="n">StringFunction</span>

<div class="viewcode-block" id="test_if_module_exists"><a class="viewcode-back" href="../../misc.html#scitools.misc.test_if_module_exists">[docs]</a><span class="k">def</span> <span class="nf">test_if_module_exists</span><span class="p">(</span><span class="n">modulename</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">,</span>
                          <span class="n">raise_exception</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">abort</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Test if modulename can be imported, and if not, write</span>
<span class="sd">    an error message and (optionally) raise an exception, continue or</span>
<span class="sd">    abort with sys.exit(1).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="nb">__import__</span><span class="p">(</span><span class="n">modulename</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">debug</span>
        <span class="n">message</span> <span class="o">=</span> <span class="s">&#39;Could not import module &quot;</span><span class="si">%s</span><span class="s">&quot; - it is &#39;</span>\
                  <span class="s">&#39;not installed on your system. </span><span class="si">%s</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> \
                  <span class="p">(</span><span class="n">modulename</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">raise_exception</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
                <span class="k">print</span> <span class="n">msg</span>
                <span class="c">#print &#39;The problem arose in &#39;,</span>
                <span class="n">debug</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">frameno</span><span class="o">=-</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="n">message</span>
                <span class="c">#print &#39;The problem arose in &#39;,</span>
                <span class="n">debug</span><span class="o">.</span><span class="n">trace</span><span class="p">(</span><span class="n">frameno</span><span class="o">=-</span><span class="mi">3</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">abort</span><span class="p">:</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">False</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="k">print</span> <span class="n">message</span>
            <span class="k">print</span> <span class="s">&#39;Got an exception while trying to import </span><span class="si">%s</span><span class="s">:</span><span class="se">\n</span><span class="s">&#39;</span> <span class="o">%</span> \
                <span class="n">modulename</span><span class="p">,</span> <span class="n">e</span>

</div>
<div class="viewcode-block" id="func_to_method"><a class="viewcode-back" href="../../misc.html#scitools.misc.func_to_method">[docs]</a><span class="k">def</span> <span class="nf">func_to_method</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">class_</span><span class="p">,</span> <span class="n">method_name</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Add a function to a class class_ as method_name.</span>
<span class="sd">    If method_name is not given, func.__name__ becomes</span>
<span class="sd">    the name of the method.</span>
<span class="sd">    Borrowed from recipe 5.12 in the Python Cookbook.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="nb">setattr</span><span class="p">(</span><span class="n">class_</span><span class="p">,</span> <span class="n">method_name</span> <span class="ow">or</span> <span class="n">func</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="system"><a class="viewcode-back" href="../../misc.html#scitools.misc.system">[docs]</a><span class="k">def</span> <span class="nf">system</span><span class="p">(</span><span class="n">command</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">failure_handling</span><span class="o">=</span><span class="s">&#39;exit&#39;</span><span class="p">,</span> <span class="n">fake</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    User-friendly wrapping of the os.system/os.popen commands.</span>
<span class="sd">    Actually, the commands.getstatusoutput function is used on Unix</span>
<span class="sd">    systems, and the output from the system command is fetched.</span>

<span class="sd">    ================  ========================================================</span>
<span class="sd">    ================  ========================================================</span>
<span class="sd">    command           operating system command to be executed</span>
<span class="sd">    verbose           False: no output, True: print command prior to execution</span>
<span class="sd">    failure_handling  one of &#39;exit&#39;, &#39;warning&#39;, &#39;exception&#39;, or &#39;silent&#39;</span>
<span class="sd">                      (in case of failure, the output from the command is</span>
<span class="sd">                      always displayed)</span>
<span class="sd">    fake              if True, the command is printed but not run (for testing)</span>
<span class="sd">    return value      the same as commands.getstatusoutput, i.e., a boolean</span>
<span class="sd">                      failure variable and the output from the command as a</span>
<span class="sd">                      string object</span>
<span class="sd">    ================  ========================================================</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;Running operating system command</span><span class="se">\n</span><span class="s">   </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">command</span>
    <span class="k">if</span> <span class="n">fake</span><span class="p">:</span>
        <span class="k">return</span> <span class="mi">0</span><span class="p">,</span> <span class="s">&#39;testing &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">command</span>

    <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;win&#39;</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">popen</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
        <span class="n">failure</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Unix/Linux/Mac:</span>
        <span class="n">failure</span><span class="p">,</span> <span class="n">output</span> <span class="o">=</span> <span class="n">commands</span><span class="o">.</span><span class="n">getstatusoutput</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">failure</span><span class="p">:</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s">&#39;Failure when running operating system command&#39;</span>\
              <span class="s">&#39;</span><span class="se">\n</span><span class="s">  </span><span class="si">%s</span><span class="se">\n</span><span class="s">Output:</span><span class="se">\n</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">command</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">failure_handling</span> <span class="o">==</span> <span class="s">&#39;exit&#39;</span><span class="p">:</span>
            <span class="k">print</span> <span class="n">msg</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">Execution aborted!&#39;</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">failure_handling</span> <span class="o">==</span> <span class="s">&#39;warning&#39;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;Warning:&#39;</span><span class="p">,</span> <span class="n">msg</span>
        <span class="k">elif</span> <span class="n">failure_handling</span> <span class="o">==</span> <span class="s">&#39;exception&#39;</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">OSError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">failure_handling</span> <span class="o">==</span> <span class="s">&#39;silent&#39;</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;wrong value &quot;</span><span class="si">%s</span><span class="s">&quot; of failure_handling&#39;</span> <span class="o">%</span> \
                             <span class="n">failure_handling</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">failure</span><span class="p">,</span> <span class="n">output</span>

</div>
<div class="viewcode-block" id="read_cml"><a class="viewcode-back" href="../../misc.html#scitools.misc.read_cml">[docs]</a><span class="k">def</span> <span class="nf">read_cml</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">argv</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Search for option (e.g. &#39;-p&#39;, &#39;--plotfile&#39;) among the command-line</span>
<span class="sd">    arguments and return the associated value (the proceeding argument).</span>
<span class="sd">    If the option is not found, the default argument is returned</span>
<span class="sd">    as str(default) (to have a unified behavior in that everything returned</span>
<span class="sd">    from read_cml is a string).</span>

<span class="sd">    The call::</span>

<span class="sd">       str2obj(read_cml(option, default=...))</span>

<span class="sd">    will return a Python object (with the right type) corresponding to</span>
<span class="sd">    the value of the object (see the str2obj function).</span>

<span class="sd">    ================  ========================================================</span>
<span class="sd">    ================  ========================================================</span>
<span class="sd">    option            command-line option (str)</span>
<span class="sd">    default           default value associated with the option</span>
<span class="sd">    argv              list that is scanned for command-line arguments</span>
<span class="sd">    return value      the item in argv after the option, or default if option</span>
<span class="sd">                      is not found</span>
<span class="sd">    ================  ========================================================</span>

<span class="sd">    See the read_cml_func function for reading function expressions</span>
<span class="sd">    or function/instance names on the command line and returning</span>
<span class="sd">    callable objects.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">index</span> <span class="o">=</span> <span class="n">argv</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">argv</span><span class="p">[</span><span class="n">index</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">default</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="s">&#39;array of command-line arguments is too short; &#39;</span>\
                         <span class="s">&#39;no value after </span><span class="si">%s</span><span class="s"> option&#39;</span> <span class="o">%</span> <span class="n">option</span><span class="p">)</span>


</div>
<div class="viewcode-block" id="str2bool"><a class="viewcode-back" href="../../misc.html#scitools.misc.str2bool">[docs]</a><span class="k">def</span> <span class="nf">str2bool</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Turn a string s, holding some boolean value</span>
<span class="sd">    (&#39;on&#39;, &#39;off&#39;, &#39;True&#39;, &#39;False&#39;, &#39;yes&#39;, &#39;no&#39; - case insensitive)</span>
<span class="sd">    into boolean variable. s can also be a boolean. Example:</span>

<span class="sd">    &gt;&gt;&gt; str2bool(&#39;OFF&#39;)</span>
<span class="sd">    False</span>
<span class="sd">    &gt;&gt;&gt; str2bool(&#39;yes&#39;)</span>
<span class="sd">    True</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">true_values</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;on&#39;</span><span class="p">,</span> <span class="s">&#39;true&#39;</span><span class="p">,</span> <span class="s">&#39;yes&#39;</span><span class="p">)</span>
        <span class="n">false_values</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;off&#39;</span><span class="p">,</span> <span class="s">&#39;false&#39;</span><span class="p">,</span> <span class="s">&#39;no&#39;</span><span class="p">)</span>
        <span class="n">s2</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>  <span class="c"># make case insensitive comparison</span>
        <span class="k">if</span> <span class="n">s2</span> <span class="ow">in</span> <span class="n">true_values</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">True</span>
        <span class="k">elif</span> <span class="n">s2</span> <span class="ow">in</span> <span class="n">false_values</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;&quot;</span><span class="si">%s</span><span class="s">&quot; is not a boolean value </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> \
                             <span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">true_values</span><span class="o">+</span><span class="n">false_values</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> cannot be converted to bool&#39;</span> <span class="o">%</span> \
                        <span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">s</span><span class="p">)))</span>

</div>
<div class="viewcode-block" id="str2obj"><a class="viewcode-back" href="../../misc.html#scitools.misc.str2obj">[docs]</a><span class="k">def</span> <span class="nf">str2obj</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">globals_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">locals_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">debug</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Turn string s into the corresponding object. str2obj is mainly</span>
<span class="sd">    used to take a string from a GUI or the command line and</span>
<span class="sd">    create a Python object. For example:</span>

<span class="sd">    &gt;&gt;&gt; s = str2obj(&#39;0.3&#39;)</span>
<span class="sd">    &gt;&gt;&gt; print s, type(s)</span>
<span class="sd">    0.3 &lt;type &#39;float&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; s = str2obj(&#39;(1,8)&#39;)</span>
<span class="sd">    &gt;&gt;&gt; print s, type(s)</span>
<span class="sd">    (1, 8) &lt;type &#39;tuple&#39;&gt;</span>

<span class="sd">    Method: eval(s) can normally do the job, but if s is meant to</span>
<span class="sd">    be turned into a string object, eval works only if s has explicit</span>
<span class="sd">    quotes:</span>

<span class="sd">    &gt;&gt;&gt; eval(&#39;some string&#39;)</span>
<span class="sd">    Traceback (most recent call last):</span>
<span class="sd">    SyntaxError: unexpected EOF while parsing</span>

<span class="sd">    (eval tries to parse &#39;some string&#39; as Python code.)</span>
<span class="sd">    Similarly, if s is a boolean word, say &#39;off&#39; or &#39;yes&#39;,</span>
<span class="sd">    eval will not work.</span>

<span class="sd">    In this function we first try to see if s is a boolean value,</span>
<span class="sd">    using scitools.misc.str2bool. If this does is not successful,</span>
<span class="sd">    we try eval(s, globals_, locals_), and if it works, we return</span>
<span class="sd">    the resulting object. Otherwise, s is (most probably) a string,</span>
<span class="sd">    so we return s itself. The None value of locals_ and globals_</span>
<span class="sd">    implies using locals() and globals() in this function.</span>

<span class="sd">    Examples:</span>

<span class="sd">    &gt;&gt;&gt; strings = (&#39;0.3&#39;, &#39;5&#39;, &#39;[-1,2]&#39;, &#39;-1+3j&#39;, &#39;dict(a=1,b=0,c=2)&#39;,</span>
<span class="sd">    ...            &#39;some string&#39;, &#39;true&#39;, &#39;ON&#39;, &#39;no&#39;)</span>
<span class="sd">    &gt;&gt;&gt; for s in strings:</span>
<span class="sd">    ...     obj = str2obj(s)</span>
<span class="sd">    ...     print &#39;&quot;%s&quot; -&gt; %s %s&#39; % (s, obj, type(obj)</span>
<span class="sd">    ...</span>
<span class="sd">    &quot;0.3&quot; -&gt; 0.3 &lt;type &#39;float&#39;&gt;</span>
<span class="sd">    &quot;5&quot; -&gt; 5 &lt;type &#39;int&#39;&gt;</span>
<span class="sd">    &quot;[-1,2]&quot; -&gt; [-1, 2] &lt;type &#39;list&#39;&gt;</span>
<span class="sd">    &quot;-1+3j&quot; -&gt; (-1+3j) &lt;type &#39;complex&#39;&gt;</span>
<span class="sd">    &quot;dict(a=1,b=0,=2)&quot; -&gt;  {&#39;a&#39;: 1, &#39;c&#39;: 2, &#39;b&#39;: 0} &lt;type &#39;dict&#39;&gt;</span>
<span class="sd">    &quot;some string&quot; -&gt; some string &lt;type &#39;str&#39;&gt;</span>
<span class="sd">    &quot;true&quot; -&gt; True &lt;type &#39;bool&#39;&gt;</span>
<span class="sd">    &quot;ON&quot; -&gt; True &lt;type &#39;bool&#39;&gt;</span>
<span class="sd">    &quot;no&quot; -&gt; False &lt;type &#39;bool&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt;</span>

<span class="sd">    If the name of a user defined function, class or instance is</span>
<span class="sd">    sent to str2obj, the calling code must also send locals() and</span>
<span class="sd">    globals() dictionaries as extra arguments. Otherwise, str2obj</span>
<span class="sd">    will not know how to &quot;eval&quot; the string and produce the right</span>
<span class="sd">    object (user-defined types are unknown inside str2obj unless</span>
<span class="sd">    the calling code&#39;s globals and locals are provided).</span>
<span class="sd">    Here is an example:</span>

<span class="sd">    &gt;&gt;&gt; def myf(x):</span>
<span class="sd">    ...     return 1+x</span>
<span class="sd">    ...</span>
<span class="sd">    &gt;&gt;&gt; class A:</span>
<span class="sd">    ...     pass</span>
<span class="sd">    ...</span>
<span class="sd">    &gt;&gt;&gt; a = A()</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; s = str2obj(&#39;myf&#39;)</span>
<span class="sd">    &gt;&gt;&gt; print s, type(s)   # now s is simply the string &#39;myf&#39;</span>
<span class="sd">    myf &lt;type &#39;str&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; # provide locals and globals such that we get the function myf:</span>
<span class="sd">    &gt;&gt;&gt; s = str2obj(&#39;myf&#39;, locals(), globals())</span>
<span class="sd">    &gt;&gt;&gt; print s, type(s)</span>
<span class="sd">    &lt;function myf at 0xb70ffe2c&gt; &lt;type &#39;function&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; s = str2obj(&#39;a&#39;, locals(), globals())</span>
<span class="sd">    &gt;&gt;&gt; print s, type(s)</span>
<span class="sd">    &lt;__main__.A instance at 0xb70f6fcc&gt; &lt;type &#39;instance&#39;&gt;</span>

<span class="sd">    With debug=True, the function will print out the exception</span>
<span class="sd">    encountered when doing eval(s, globals_, locals_), and this may</span>
<span class="sd">    point out problems with, e.g., imports in the calling code</span>
<span class="sd">    (insufficient variables in globals_).</span>

<span class="sd">    Note: if the string argument is the name of a valid Python</span>
<span class="sd">    class (type), that class will be returned. For example,</span>
<span class="sd">    &gt;&gt;&gt; str2obj(&#39;list&#39;)  # returns class list</span>
<span class="sd">    &lt;type &#39;list&#39;&gt;</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">globals_</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">globals_</span> <span class="o">=</span> <span class="nb">globals</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">locals_</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">locals_</span> <span class="o">=</span> <span class="nb">locals</span><span class="p">()</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">str2bool</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">b</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">),</span> <span class="n">e</span><span class="p">:</span>
        <span class="c"># s is not a boolean value, try eval(s):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">b</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">globals_</span><span class="p">,</span> <span class="n">locals_</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">b</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">debug</span><span class="p">:</span>
                <span class="k">print</span> <span class="s">&quot;&quot;&quot;</span>
<span class="s">scitools.misc.str2obj:</span>
<span class="s">Tried to do eval(s) with s=&quot;</span><span class="si">%s</span><span class="s">&quot;, and it resulted in an exception:</span>
<span class="s">    </span><span class="si">%s</span><span class="s"></span>
<span class="s">&quot;&quot;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
            <span class="c"># eval(s) did not work, s is probably a string:</span>
            <span class="k">return</span> <span class="n">s</span>

</div>
<div class="viewcode-block" id="str2type"><a class="viewcode-back" href="../../misc.html#scitools.misc.str2type">[docs]</a><span class="k">def</span> <span class="nf">str2type</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a function that can take a string and convert it to</span>
<span class="sd">    a Python object of the same type as value.</span>

<span class="sd">    This function is useful when turning input from GUIs or the</span>
<span class="sd">    command line into Python objects. Given a default value for the</span>
<span class="sd">    input (with the right object type), str2type will return the right</span>
<span class="sd">    conversion function.  (str2obj can do the thing, but will often</span>
<span class="sd">    return eval, which turns any string into a Python object - this is</span>
<span class="sd">    less safe than str2type, which never returns eval. That principle</span>
<span class="sd">    helps to detect wrong input.)</span>

<span class="sd">    Method: If value is bool, we use scitools.misc.str2bool, which</span>
<span class="sd">    is capable of converting strings like &quot;on&quot;, &quot;off&quot;,&quot;yes&quot;, &quot;no&quot;,</span>
<span class="sd">    &quot;true&quot; and &quot;false&quot; to boolean values. Otherwise, we use</span>
<span class="sd">    type(value) as the conversion function. However, there is one</span>
<span class="sd">    problem with type(value) when value is an int while the user</span>
<span class="sd">    intended a general real number - in that case one may get</span>
<span class="sd">    wrong answers because of wrong (int) round off. Another problem</span>
<span class="sd">    concerns user-defined types. For those (which str2type knows</span>
<span class="sd">    nothing about) the str function is returned, implying that the</span>
<span class="sd">    conversion from a string to the right user-defined type cannot</span>
<span class="sd">    be done by the function returned from str2type.</span>

<span class="sd">    Examples:</span>

<span class="sd">    &gt;&gt;&gt; f = str2type((1,4,3))</span>
<span class="sd">    &gt;&gt;&gt; f.__name__</span>
<span class="sd">    &#39;tuple&#39;</span>

<span class="sd">    &gt;&gt;&gt; f = str2type(MySpecialClass())</span>
<span class="sd">    &gt;&gt;&gt; f.__name__</span>
<span class="sd">    &#39;str&#39;</span>

<span class="sd">    &gt;&gt;&gt; f = str2type(&#39;some string&#39;)</span>
<span class="sd">    &gt;&gt;&gt; f.__name__</span>
<span class="sd">    &#39;str&#39;</span>

<span class="sd">    &gt;&gt;&gt; f = str2type(False)</span>
<span class="sd">    &gt;&gt;&gt; f.__name__</span>
<span class="sd">    &#39;str2bool&#39;</span>

<span class="sd">    (Note that we could return eval if value is not a string or a boolean,</span>
<span class="sd">    but eval is never returned from this function to avoid conversion</span>
<span class="sd">    to an unintended type.)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">str2bool</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">basestring</span><span class="p">,</span><span class="nb">int</span><span class="p">,</span><span class="nb">float</span><span class="p">,</span><span class="nb">complex</span><span class="p">,</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">,</span><span class="nb">dict</span><span class="p">)):</span>
        <span class="k">return</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># the type of value is probably defined in some unknown module</span>
        <span class="k">return</span> <span class="nb">str</span>

</div>
<div class="viewcode-block" id="interpret_as_callable_or_StringFunction"><a class="viewcode-back" href="../../misc.html#scitools.misc.interpret_as_callable_or_StringFunction">[docs]</a><span class="k">def</span> <span class="nf">interpret_as_callable_or_StringFunction</span><span class="p">(</span>
    <span class="n">s</span><span class="p">,</span> <span class="n">iv</span><span class="p">,</span> <span class="n">globals_</span><span class="p">,</span> <span class="o">**</span><span class="n">named_parameters</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a callable object if ``s`` is the name of such an</span>
<span class="sd">    object, otherwise turn ``s`` to a ``StringFunction`` object with</span>
<span class="sd">    ``iv`` as the name of the independent variable.</span>
<span class="sd">    The ``named_parameters`` dictionary holds parameters in</span>
<span class="sd">    string expressions.</span>
<span class="sd">    Used by the ``read_cml`` function.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">globals_</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
        <span class="c"># First assume s is a user defined function or instance creation</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">globals_</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
                    <span class="k">return</span> <span class="n">obj</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">s_is_string</span> <span class="o">=</span> <span class="bp">True</span>
            <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span>
                <span class="n">s_is_string</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>  <span class="c"># user function obj or lambda func obj</span>
                <span class="k">return</span> <span class="n">s</span>

    <span class="k">elif</span> <span class="n">globals_</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="c"># No global names supplied, s cannot be the string of a</span>
        <span class="c"># user function or instance</span>
        <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">s</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">s_is_string</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;globals_ must dict or None&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">s_is_string</span><span class="p">:</span>
        <span class="c"># Assume that s is a string expression</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">iv</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>  <span class="c"># single indep. variable?</span>
            <span class="n">iv</span> <span class="o">=</span> <span class="p">[</span><span class="n">iv</span><span class="p">]</span>
        <span class="n">func</span> <span class="o">=</span> <span class="n">StringFunction</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">independent_variables</span><span class="o">=</span><span class="n">iv</span><span class="p">,</span>
                              <span class="nb">globals</span><span class="o">=</span><span class="n">globals_</span><span class="p">,</span> <span class="o">**</span><span class="n">named_parameters</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">func</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Should never come here</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;s is neither a string expression, nor a callable </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>


</div>
<div class="viewcode-block" id="read_cml_func"><a class="viewcode-back" href="../../misc.html#scitools.misc.read_cml_func">[docs]</a><span class="k">def</span> <span class="nf">read_cml_func</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">default_func</span><span class="p">,</span> <span class="n">iv</span><span class="o">=</span><span class="s">&#39;t&#39;</span><span class="p">,</span> <span class="n">globals_</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span>
                  <span class="o">**</span><span class="n">named_parameters</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Locate ``--option`` on the command line (``sys.argv``) and find</span>
<span class="sd">    the corresponding value (next ``sys.argv`` element).</span>
<span class="sd">    This value is supposed to specify a Python function, an</span>
<span class="sd">    instance with a ``__call__`` method, None, or a string that can be</span>
<span class="sd">    turned into a ``scitools.StringFunction.StringFunction``</span>
<span class="sd">    function with ``iv`` as the name of the independent variable(s)</span>
<span class="sd">    (list of strings in case of more than one independent variable).</span>
<span class="sd">    If ``--option`` is not found, the argument ``default_func``,</span>
<span class="sd">    a given callable or string, is returned (if string, ``iv``</span>
<span class="sd">    reflects the name of the independent variable(s) in the</span>
<span class="sd">    string).</span>

<span class="sd">    The ``globals_`` argument is just passed on to the</span>
<span class="sd">    ``StringFunction`` object if the value of the option for default</span>
<span class="sd">    function is a string. Similary, the ``named_parameters`` dictionary</span>
<span class="sd">    is passed on to the ``StringFunction`` object and assumed to</span>
<span class="sd">    hold parameters in the string expressions (variables different</span>
<span class="sd">    from the independent variable).</span>

<span class="sd">    This function always returns a callable object or None.</span>

<span class="sd">    Here is an interactive session showing the use of ``read_cml_func``::</span>

<span class="sd">    &gt;&gt;&gt; from scitools.misc import read_cml_func</span>
<span class="sd">    &gt;&gt;&gt; import sys</span>
<span class="sd">    &gt;&gt;&gt; from math import sin, cos, pi</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; # fake command-line arguments by filling in sys.argv:</span>
<span class="sd">    &gt;&gt;&gt; sys.argv[1:] = &#39;--func1 myfunc --func3 sin(x)&#39;.split()</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; def myfunc(x):</span>
<span class="sd">    ...     return 1 + x</span>
<span class="sd">    ...</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; # --func1 has myfunc as value, must pass on globals() or just</span>
<span class="sd">    &gt;&gt;&gt; # myfunc as name to read_cml_func</span>
<span class="sd">    &gt;&gt;&gt; #f = read_cml_func(&#39;--func1&#39;, &#39;1&#39;, globals_=globals())</span>
<span class="sd">    &gt;&gt;&gt; f = read_cml_func(&#39;--func1&#39;, &#39;1&#39;, globals_={&#39;myfunc&#39;: myfunc})</span>
<span class="sd">    &gt;&gt;&gt; type(f)</span>
<span class="sd">    &lt;type &#39;function&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; f(10)</span>
<span class="sd">    11</span>
<span class="sd">    &gt;&gt;&gt; # --func3 is given as a string expression &quot;sin(x)&quot; on the command line</span>
<span class="sd">    &gt;&gt;&gt; f = read_cml_func(&#39;--func3&#39;, &#39;0&#39;, iv=&#39;x&#39;)</span>
<span class="sd">    &gt;&gt;&gt; type(f)</span>
<span class="sd">    &lt;type &#39;instance&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; repr(f)</span>
<span class="sd">    &quot;StringFunction(&#39;sin(x)&#39;, independent_variables=(&#39;x&#39;,), )&quot;</span>
<span class="sd">    &gt;&gt;&gt; str(f)</span>
<span class="sd">    &#39;sin(x)&#39;</span>
<span class="sd">    &gt;&gt;&gt; f(pi)</span>
<span class="sd">    1.2246467991473532e-16</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; # --func2 is not given on the command line, use the default</span>
<span class="sd">    &gt;&gt;&gt; # value &quot;A*cos(w*t)&quot;, which is a string expression.</span>
<span class="sd">    &gt;&gt;&gt; # Pass on a globals_ dict with cos from numpy such that f works</span>
<span class="sd">    &gt;&gt;&gt; # with array argument for t</span>
<span class="sd">    &gt;&gt;&gt; import numpy</span>
<span class="sd">    &gt;&gt;&gt; f = read_cml_func(&#39;--func2&#39;, &#39;A*cos(w*t)&#39;, iv=&#39;t&#39;, A=3, w=pi, globals_={&#39;cos&#39;: numpy.cos})</span>
<span class="sd">    &gt;&gt;&gt; # More general case where the string should have all numpy functions:</span>
<span class="sd">    &gt;&gt;&gt; #f = read_cml_func(&#39;--func2&#39;, &#39;A*cos(w*t)&#39;, iv=&#39;t&#39;, A=3, w=pi, globals_=dir(numpy))</span>
<span class="sd">    &gt;&gt;&gt; type(f)</span>
<span class="sd">    &lt;type &#39;instance&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; repr(f)</span>
<span class="sd">    &quot;StringFunction(&#39;A*cos(w*t)&#39;, independent_variables=(&#39;t&#39;,), A=3, w=3.141592653589793)&quot;</span>
<span class="sd">    &gt;&gt;&gt; str(f)</span>
<span class="sd">    &#39;3*cos(3.14159265359*t)&#39;</span>
<span class="sd">    &gt;&gt;&gt; t = numpy.array([1, 4])</span>
<span class="sd">    &gt;&gt;&gt; f(t)</span>
<span class="sd">    array([-3.,  3.])</span>
<span class="sd">    &gt;&gt;&gt;</span>
<span class="sd">    &gt;&gt;&gt; # No --func4 on the command line (sys.argv), implying that</span>
<span class="sd">    &gt;&gt;&gt; # f becomes a StringFunction with value 0</span>
<span class="sd">    &gt;&gt;&gt; f = read_cml_func(&#39;--func4&#39;, &#39;0&#39;)</span>
<span class="sd">    &gt;&gt;&gt; type(f)</span>
<span class="sd">    &lt;type &#39;instance&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; repr(f)</span>
<span class="sd">    &quot;StringFunction(&#39;0&#39;, independent_variables=(&#39;t&#39;,), )&quot;</span>
<span class="sd">    &gt;&gt;&gt; str(f)</span>
<span class="sd">    &#39;0&#39;</span>
<span class="sd">    &gt;&gt;&gt; f(1)</span>
<span class="sd">    0</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">:</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">option</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span>
                <span class="s">&#39;no value after option </span><span class="si">%s</span><span class="s"> on the command line&#39;</span> \
                <span class="o">%</span> <span class="n">option</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">interpret_as_callable_or_StringFunction</span><span class="p">(</span>
            <span class="n">value</span><span class="p">,</span> <span class="n">iv</span><span class="p">,</span> <span class="n">globals_</span><span class="o">=</span><span class="n">globals_</span><span class="p">,</span> <span class="o">**</span><span class="n">named_parameters</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">default_func</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Assume default_func is string expression or a</span>
            <span class="c"># callable function listed in the globals_ dict</span>
            <span class="c"># or just a lambda function</span>
            <span class="k">return</span> <span class="n">interpret_as_callable_or_StringFunction</span><span class="p">(</span>
                <span class="n">default_func</span><span class="p">,</span> <span class="n">iv</span><span class="p">,</span> <span class="n">globals_</span><span class="o">=</span><span class="n">globals_</span><span class="p">,</span> <span class="o">**</span><span class="n">named_parameters</span><span class="p">)</span>


</div>
<div class="viewcode-block" id="function_UI"><a class="viewcode-back" href="../../misc.html#scitools.misc.function_UI">[docs]</a><span class="k">def</span> <span class="nf">function_UI</span><span class="p">(</span><span class="n">functions</span><span class="p">,</span> <span class="n">argv</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    User interface for calling a collection of functions from the</span>
<span class="sd">    command line by writing the function name and its arguments.</span>
<span class="sd">    functions is a list of possible functions to be called. argv is</span>
<span class="sd">    sys.argv from the calling code.</span>
<span class="sd">    function_UI returns a command to be evaluated (function call)</span>
<span class="sd">    in the calling code.</span>

<span class="sd">    This function autogenerates a user interface to a module.</span>
<span class="sd">    Suppose a module has a set of functions::</span>

<span class="sd">      test_mymethod1(M, q, a, b)</span>
<span class="sd">      test_method2a(a1, a2, a3=0, doc=None)</span>
<span class="sd">      test_method2b()</span>

<span class="sd">    The following code automatically creates a user interface</span>
<span class="sd">    and executes calls to the functions above::</span>

<span class="sd">      from scitools.misc import function_UI</span>
<span class="sd">      function_names = [fname for fname in dir() if fname.startswith(&#39;test_&#39;)]</span>
<span class="sd">      function_UI(function_names, sys.argv)</span>

<span class="sd">    On the command line the user can now type::</span>

<span class="sd">      programname --help</span>

<span class="sd">    and automatically get a help string for each function, consisting</span>
<span class="sd">    of the function name, all its positional arguments and all its</span>
<span class="sd">    keyword arguments.</span>

<span class="sd">    Alternatively, writing just the function name::</span>

<span class="sd">      programname functionname</span>

<span class="sd">    prints a usage string if this function requires arguments, otherwise</span>
<span class="sd">    the function is just called.</span>

<span class="sd">    Finally, when arguments are supplied::</span>

<span class="sd">      programname functionname arg1 arg2 arg3 ...</span>

<span class="sd">    the function is called with the given arguments. Safe and easy use</span>
<span class="sd">    is ensured by always giving keyword arguments::</span>

<span class="sd">      programname functionname arg1=value1 arg2=value2 arg3=value3 ...</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">usage</span><span class="p">,</span> <span class="n">doc</span> <span class="o">=</span> <span class="n">_function_args_doc</span><span class="p">(</span><span class="n">functions</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">all_usage</span><span class="p">():</span>
        <span class="k">for</span> <span class="n">fname</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">usage</span><span class="p">):</span>
            <span class="k">print</span> <span class="n">fname</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">usage</span><span class="p">[</span><span class="n">fname</span><span class="p">])</span>

    <span class="c"># call: function-name arg1 arg2 ...</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">all_usage</span><span class="p">()</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="n">function_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">__name__</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">functions</span><span class="p">]</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">in</span> <span class="n">function_names</span> <span class="ow">and</span> <span class="n">usage</span><span class="p">[</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]]:</span>
        <span class="n">fname</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="k">print</span> <span class="s">&#39;Usage:&#39;</span><span class="p">,</span> <span class="n">fname</span><span class="p">,</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">usage</span><span class="p">[</span><span class="n">fname</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">doc</span><span class="p">[</span><span class="n">fname</span><span class="p">]:</span>
            <span class="k">print</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">Docstring:</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="n">doc</span><span class="p">[</span><span class="n">fname</span><span class="p">]</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">argv</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span> <span class="ow">and</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s">&#39;help&#39;</span><span class="p">):</span>
        <span class="n">all_usage</span><span class="p">()</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="n">cmd</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">:]))</span>
    <span class="c">#if len(argv[2:]) == len(usage[fname]):</span>
        <span class="c"># Correct no arguments (eh, can leave out keyword args...)</span>
    <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;Calling&#39;</span><span class="p">,</span> <span class="n">cmd</span>
    <span class="k">return</span> <span class="n">cmd</span>

</div>
<span class="k">def</span> <span class="nf">_function_args_doc</span><span class="p">(</span><span class="n">functions</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create documentation of a list of functions.</span>
<span class="sd">    Return: usage dict (usage[funcname] = list of arguments, incl.</span>
<span class="sd">    default values), doc dict (doc[funcname] = docstring (or None)).</span>
<span class="sd">    Called by function_UI.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">inspect</span>
    <span class="n">usage</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">doc</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">functions</span><span class="p">:</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">args</span><span class="o">.</span><span class="n">defaults</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># Only positional arguments</span>
            <span class="n">usage</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">args</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Keyword arguments too, build complete list</span>
            <span class="n">usage</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">__name__</span><span class="p">]</span> <span class="o">=</span> <span class="n">args</span><span class="o">.</span><span class="n">args</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">defaults</span><span class="p">)]</span> <span class="o">+</span> \
                     <span class="p">[</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> \
                      <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="o">.</span><span class="n">defaults</span><span class="p">):],</span> <span class="n">args</span><span class="o">.</span><span class="n">defaults</span><span class="p">)]</span>
        <span class="n">doc</span><span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">__name__</span><span class="p">]</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">usage</span><span class="p">,</span> <span class="n">doc</span>


<div class="viewcode-block" id="before"><a class="viewcode-back" href="../../misc.html#scitools.misc.before">[docs]</a><span class="k">def</span> <span class="nf">before</span><span class="p">(</span><span class="n">string</span><span class="p">,</span> <span class="n">character</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return part of string before character.&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">string</span><span class="p">)):</span>
        <span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="n">character</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">string</span><span class="p">[:</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
</div>
<div class="viewcode-block" id="after"><a class="viewcode-back" href="../../misc.html#scitools.misc.after">[docs]</a><span class="k">def</span> <span class="nf">after</span><span class="p">(</span><span class="n">string</span><span class="p">,</span> <span class="n">character</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return part of string after character.&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">string</span><span class="p">)):</span>
        <span class="k">if</span> <span class="n">c</span> <span class="o">==</span> <span class="n">character</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">string</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
</div>
<div class="viewcode-block" id="remove_multiple_items"><a class="viewcode-back" href="../../misc.html#scitools.misc.remove_multiple_items">[docs]</a><span class="k">def</span> <span class="nf">remove_multiple_items</span><span class="p">(</span><span class="n">somelist</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given some list somelist, return a list where identical items</span>
<span class="sd">    are removed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">right_type</span><span class="p">(</span><span class="n">somelist</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span>
    <span class="n">new</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">helphash</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">somelist</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">helphash</span><span class="p">:</span>
            <span class="n">new</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
            <span class="n">helphash</span><span class="p">[</span><span class="n">item</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">new</span>

</div>
<div class="viewcode-block" id="find"><a class="viewcode-back" href="../../misc.html#scitools.misc.find">[docs]</a><span class="k">def</span> <span class="nf">find</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">rootdir</span><span class="p">,</span> <span class="n">arg</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Traverse the directory tree rootdir and call func for each file.</span>
<span class="sd">    arg is a user-provided argument transferred to func(filename,arg).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">rootdir</span><span class="p">)</span>  <span class="c"># get all files in rootdir</span>
    <span class="n">files</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">:</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span><span class="n">b</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
    <span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
        <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span><span class="nb">file</span><span class="p">)</span> <span class="c"># make complete path</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">islink</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
            <span class="k">pass</span> <span class="c"># drop links...</span>
        <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
            <span class="n">find</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">fullpath</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="c"># recurse into directory</span>
        <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
            <span class="n">func</span><span class="p">(</span><span class="n">fullpath</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="c"># file is regular, apply func</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;find: cannot treat &#39;</span><span class="p">,</span> <span class="n">fullpath</span>

</div>
<div class="viewcode-block" id="sorted_os_path_walk"><a class="viewcode-back" href="../../misc.html#scitools.misc.sorted_os_path_walk">[docs]</a><span class="k">def</span> <span class="nf">sorted_os_path_walk</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Like os.path.walk, but directories and files are visited and</span>
<span class="sd">    listed in alphabetic order.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">files</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>  <span class="c"># get all files in rootdir</span>
    <span class="k">except</span> <span class="n">os</span><span class="o">.</span><span class="n">error</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">files</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">:</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">b</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
    <span class="n">func</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">root</span><span class="p">,</span> <span class="n">files</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
            <span class="n">sorted_os_path_walk</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span> <span class="c"># recurse into directory</span>

</div>
<div class="viewcode-block" id="subst"><a class="viewcode-back" href="../../misc.html#scitools.misc.subst">[docs]</a><span class="k">def</span> <span class="nf">subst</span><span class="p">(</span><span class="n">patterns</span><span class="p">,</span> <span class="n">replacements</span><span class="p">,</span> <span class="n">filenames</span><span class="p">,</span>
          <span class="n">pattern_matching_modifiers</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Replace a set of patterns by a set of replacement strings (regular</span>
<span class="sd">    expressions) in a series of files.</span>
<span class="sd">    The function essentially performs::</span>

<span class="sd">      for filename in filenames:</span>
<span class="sd">          file_string = open(filename, &#39;r&#39;).read()</span>
<span class="sd">          for pattern, replacement in zip(patterns, replacements):</span>
<span class="sd">              file_string = re.sub(pattern, replacement, file_string)</span>

<span class="sd">    A copy of the original file is taken, with extension `.old~`.</span>

<span class="sd">    ==========================  ======================================</span>
<span class="sd">    ==========================  ======================================</span>
<span class="sd">    patterns                    string or list of strings (regex)</span>
<span class="sd">    replacements                string or list of strings (regex)</span>
<span class="sd">    filenames                   string or list of strings</span>
<span class="sd">    pattern_matching_modifiers  re.DOTALL, re.MULTILINE, etc., same</span>
<span class="sd">                                syntax as for re.compile</span>
<span class="sd">    ==========================  ======================================</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># if some arguments are strings, convert them to lists:</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">patterns</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">):</span>
        <span class="n">patterns</span> <span class="o">=</span> <span class="p">[</span><span class="n">patterns</span><span class="p">]</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">replacements</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">):</span>
        <span class="n">replacements</span> <span class="o">=</span> <span class="p">[</span><span class="n">replacements</span><span class="p">]</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filenames</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">):</span>
        <span class="n">filenames</span> <span class="o">=</span> <span class="p">[</span><span class="n">filenames</span><span class="p">]</span>

    <span class="c"># pre-compile patterns:</span>
    <span class="n">cpatterns</span> <span class="o">=</span> <span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">pattern_matching_modifiers</span><span class="p">)</span> \
                 <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">patterns</span><span class="p">]</span>
    <span class="n">modified_files</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">p</span><span class="p">,[])</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">patterns</span><span class="p">])</span>  <span class="c"># init</span>
    <span class="n">messages</span> <span class="o">=</span> <span class="p">[]</span>   <span class="c"># for return info</span>

    <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">filenames</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> is not a file!&#39;</span> <span class="o">%</span> <span class="n">filename</span><span class="p">)</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">);</span>
        <span class="n">filestr</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">pattern</span><span class="p">,</span> <span class="n">cpattern</span><span class="p">,</span> <span class="n">replacement</span> <span class="ow">in</span> \
            <span class="nb">zip</span><span class="p">(</span><span class="n">patterns</span><span class="p">,</span> <span class="n">cpatterns</span><span class="p">,</span> <span class="n">replacements</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">cpattern</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">filestr</span><span class="p">):</span>
                <span class="n">filestr</span> <span class="o">=</span> <span class="n">cpattern</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">replacement</span><span class="p">,</span> <span class="n">filestr</span><span class="p">)</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copy2</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">filename</span> <span class="o">+</span> <span class="s">&#39;.old~&#39;</span><span class="p">)</span> <span class="c"># backup</span>
                <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
                <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">filestr</span><span class="p">)</span>
                <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="n">modified_files</span><span class="p">[</span><span class="n">pattern</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>

    <span class="c"># make a readable return string with substitution info:</span>
    <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">modified_files</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">modified_files</span><span class="p">[</span><span class="n">pattern</span><span class="p">]:</span>
            <span class="n">replacement</span> <span class="o">=</span> <span class="n">replacements</span><span class="p">[</span><span class="n">patterns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">pattern</span><span class="p">)]</span>
            <span class="n">messages</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> replaced by </span><span class="si">%s</span><span class="s"> in </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> \
                                <span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">replacement</span><span class="p">,</span>
                                 <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">modified_files</span><span class="p">[</span><span class="n">pattern</span><span class="p">])))</span>

    <span class="k">return</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">messages</span><span class="p">)</span> <span class="k">if</span> <span class="n">messages</span> <span class="k">else</span> <span class="s">&#39;no substitutions&#39;</span>


<span class="c"># class Command has now been replaced by the standard functools.partial</span>
<span class="c"># function in Python v2.5 and later:</span>
</div>
<div class="viewcode-block" id="Command"><a class="viewcode-back" href="../../misc.html#scitools.misc.Command">[docs]</a><span class="k">class</span> <span class="nc">Command</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Alternative to lambda functions.</span>

<span class="sd">    This class should with Python version 2.5 and later be replaced</span>
<span class="sd">    by functools.partial.</span>
<span class="sd">    However, you cannot simply do a::</span>

<span class="sd">      Command = functools.partial</span>

<span class="sd">    to be backward compatible with your old programs that use Command,</span>
<span class="sd">    because Command and functools.partial supply the positional</span>
<span class="sd">    arguments in different manners: Command calls the underlying</span>
<span class="sd">    function with new arguments followed by the originally recorded</span>
<span class="sd">    arguments, while functools.partial does it the other way around</span>
<span class="sd">    (first original arguments, then new positional arguments).</span>

<span class="sd">    This Command class is kept for backward compatibility. New usage</span>
<span class="sd">    should employ functools.partial instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">args</span> <span class="o">=</span> <span class="n">args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">args</span> <span class="o">=</span> <span class="n">args</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="timer"><a class="viewcode-back" href="../../misc.html#scitools.misc.timer">[docs]</a><span class="k">def</span> <span class="nf">timer</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[],</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{},</span> <span class="n">repetitions</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run a function func, with arguments given by the tuple</span>
<span class="sd">    args and keyword arguments given by the dictionary kwargs,</span>
<span class="sd">    a specified number of times (repetitions) and</span>
<span class="sd">    write out the elapsed time and the CPU time together.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">t0</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">();</span>  <span class="n">c0</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">clock</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">repetitions</span><span class="p">):</span>
        <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="n">cpu_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">clock</span><span class="p">()</span><span class="o">-</span><span class="n">c0</span>
    <span class="n">elapsed_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">t0</span>
    <span class="k">try</span><span class="p">:</span>    <span class="c"># instance method?</span>
        <span class="n">name</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="n">im_class</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s">&#39;.&#39;</span> <span class="o">+</span> <span class="n">func</span><span class="o">.</span><span class="n">__name__</span>
    <span class="k">except</span><span class="p">:</span> <span class="c"># ordinary function</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">func</span><span class="o">.</span><span class="n">__name__</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> (</span><span class="si">%d</span><span class="s"> calls): elapsed=</span><span class="si">%g</span><span class="s">, CPU=</span><span class="si">%g</span><span class="s">&#39;</span> <span class="o">%</span> \
          <span class="p">(</span><span class="n">comment</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">repetitions</span><span class="p">,</span> <span class="n">elapsed_time</span><span class="p">,</span> <span class="n">cpu_time</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">cpu_time</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">repetitions</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="timer_system"><a class="viewcode-back" href="../../misc.html#scitools.misc.timer_system">[docs]</a><span class="k">def</span> <span class="nf">timer_system</span><span class="p">(</span><span class="n">command</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run an os.system(command) statement and measure the CPU time.</span>
<span class="sd">    With os.system, the CPU time is registered as the user and</span>
<span class="sd">    system time of child processes.</span>

<span class="sd">    Note: there might be some overhead in the timing compared to</span>
<span class="sd">    running time in the OS instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">t0</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">times</span><span class="p">()</span>
    <span class="n">failure</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">system</span><span class="p">(</span><span class="n">command</span><span class="p">)</span>
    <span class="n">t1</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">times</span><span class="p">()</span>
    <span class="c"># some programs return nonzero even when they work (grep, for inst.)</span>
    <span class="k">if</span> <span class="n">failure</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&#39;Note: os.system(</span><span class="si">%s</span><span class="s">) failed&#39;</span> <span class="o">%</span> <span class="n">command</span><span class="p">,</span> <span class="s">&#39;returned&#39;</span><span class="p">,</span> <span class="n">failure</span>
    <span class="n">cpu_time</span> <span class="o">=</span> <span class="n">t1</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">-</span><span class="n">t0</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">t1</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">-</span><span class="n">t0</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s"> system command: &quot;</span><span class="si">%s</span><span class="s">&quot;: elapsed=</span><span class="si">%g</span><span class="s"> CPU=</span><span class="si">%g</span><span class="s">&#39;</span> <span class="o">%</span> \
          <span class="p">(</span><span class="n">comment</span><span class="p">,</span> <span class="n">command</span><span class="p">,</span> <span class="n">t1</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span><span class="o">-</span><span class="n">t0</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">cpu_time</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">cpu_time</span>

</div>
<div class="viewcode-block" id="findprograms"><a class="viewcode-back" href="../../misc.html#scitools.misc.findprograms">[docs]</a><span class="k">def</span> <span class="nf">findprograms</span><span class="p">(</span><span class="n">programs</span><span class="p">,</span> <span class="n">searchlibs</span><span class="o">=</span><span class="p">[],</span> <span class="n">write_message</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a list of programs (programs), find the full path</span>
<span class="sd">    of each program and return a dictionary with the program</span>
<span class="sd">    name as key and the full path as value. The value is None</span>
<span class="sd">    if the program is not found.</span>

<span class="sd">    The program list can either be a list/tuple or a</span>
<span class="sd">    dictionary (in the latter case, the keys are the programs</span>
<span class="sd">    and the values are explanations of the programs).</span>
<span class="sd">    If write_message is true, the function writes a message</span>
<span class="sd">    if a program is not found. In that case, None is returned</span>
<span class="sd">    if not all programs are found.</span>

<span class="sd">    A single program can also be given as first argument. In that</span>
<span class="sd">    case, findprograms returns True or False according to whether</span>
<span class="sd">    the program is found or not.</span>

<span class="sd">    Example on usage::</span>

<span class="sd">      if findprograms(&#39;plotmtv&#39;):</span>
<span class="sd">          os.system(&#39;plotmtv ...&#39;)</span>

<span class="sd">      # write a message if a program is not found:</span>
<span class="sd">      if findprograms([&#39;plotmtv&#39;], write_message=True):</span>
<span class="sd">          os.system(&#39;plotmtv ...&#39;)</span>

<span class="sd">      programs = [&#39;gs&#39;, &#39;convert&#39;]</span>
<span class="sd">      path = findprograms(programs)</span>
<span class="sd">      if path[&#39;gs&#39;]:</span>
<span class="sd">          os.system(&#39;gs ...&#39;)</span>
<span class="sd">      if path[&#39;convert&#39;]:</span>
<span class="sd">          os.system(&#39;convert ...&#39;)</span>

<span class="sd">      programs = { &#39;gs&#39; : &#39;Ghostscript: file format conversions&#39;,</span>
<span class="sd">                   &#39;convert&#39; : &#39;File format conversion from ImageMagick&#39;,</span>
<span class="sd">                 }</span>
<span class="sd">      if not findprograms(programs, write_message=True):</span>
<span class="sd">          print &#39;the mentioned programs need to be installed&#39;</span>
<span class="sd">          sys.exit(1)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">program_exists</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;win&#39;</span><span class="p">):</span>
            <span class="c"># add .exe or .bat to program filename:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="o">+</span><span class="s">&#39;.exe&#39;</span><span class="p">)</span> <span class="ow">or</span> \
               <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="o">+</span><span class="s">&#39;.bat&#39;</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">True</span>
        <span class="k">elif</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s">&#39;posix&#39;</span><span class="p">:</span> <span class="c"># Unix</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s">&#39;platform </span><span class="si">%s</span><span class="s">/</span><span class="si">%s</span><span class="s"> not supported&#39;</span> <span class="o">%</span> \
                            <span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">False</span> <span class="c"># otherwise</span>

    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;PATH&#39;</span><span class="p">]</span>  <span class="c"># /usr/bin:/usr/local/bin:/usr/X11/bin</span>
    <span class="n">paths</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">pathsep</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
    <span class="n">fullpaths</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">programs</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">program</span> <span class="o">=</span> <span class="n">programs</span>
        <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span> <span class="c"># skip non-existing directories</span>
                <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">program</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">program_exists</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                    <span class="k">return</span> <span class="bp">True</span>
        <span class="c"># else, not found:</span>
        <span class="k">if</span> <span class="n">write_message</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;program </span><span class="si">%s</span><span class="s"> not found&#39;</span> <span class="o">%</span> <span class="n">programs</span>
        <span class="k">return</span> <span class="bp">False</span>

    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">programs</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span><span class="nb">tuple</span><span class="p">)):</span>
        <span class="c"># initialize with None:</span>
        <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">programs</span><span class="p">:</span>  <span class="n">fullpaths</span><span class="p">[</span><span class="n">program</span><span class="p">]</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">programs</span><span class="p">:</span>
            <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span> <span class="c"># skip non-existing directories</span>
                    <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">program</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">program_exists</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                        <span class="n">fullpaths</span><span class="p">[</span><span class="n">program</span><span class="p">]</span> <span class="o">=</span> <span class="n">fullpath</span>
                        <span class="k">break</span>  <span class="c"># stop when the program is found</span>

    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">programs</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
        <span class="c"># initialize with None:</span>
        <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">programs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>  <span class="n">fullpaths</span><span class="p">[</span><span class="n">program</span><span class="p">]</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">programs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span> <span class="c"># skip non-existing directories</span>
                    <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">program</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">program_exists</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                        <span class="n">fullpaths</span><span class="p">[</span><span class="n">program</span><span class="p">]</span> <span class="o">=</span> <span class="n">fullpath</span>
                        <span class="k">break</span>

    <span class="k">if</span> <span class="n">write_message</span><span class="p">:</span>
        <span class="n">missing</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">fullpaths</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">fullpaths</span><span class="p">[</span><span class="n">program</span><span class="p">]:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">program</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
                    <span class="k">print</span> <span class="s">&quot;program &#39;</span><span class="si">%s</span><span class="s">&#39; (</span><span class="si">%s</span><span class="s">) not found&quot;</span> <span class="o">%</span> \
                          <span class="p">(</span><span class="n">program</span><span class="p">,</span><span class="n">programs</span><span class="p">[</span><span class="n">program</span><span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>  <span class="c"># list or tuple</span>
                    <span class="k">print</span> <span class="s">&#39;program &quot;</span><span class="si">%s</span><span class="s">&quot; not found&#39;</span> <span class="o">%</span> <span class="n">program</span>
                <span class="n">missing</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">if</span> <span class="n">missing</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">None</span>

    <span class="k">return</span> <span class="n">fullpaths</span>

</div>
<div class="viewcode-block" id="pathsearch"><a class="viewcode-back" href="../../misc.html#scitools.misc.pathsearch">[docs]</a><span class="k">def</span> <span class="nf">pathsearch</span><span class="p">(</span><span class="n">programs</span><span class="o">=</span><span class="p">[],</span> <span class="n">modules</span><span class="o">=</span><span class="p">[],</span> <span class="n">where</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a list of programs (programs) and modules (modules),</span>
<span class="sd">    search for these programs and modules in the directories</span>
<span class="sd">    in the PATH and PYTHONPATH environment variables, respectively.</span>
<span class="sd">    Check that each directory has read and write access too.</span>
<span class="sd">    The function is useful for checking that PATH and PYTHONPATH</span>
<span class="sd">    are appropriately set in CGI scripts.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;PATH&#39;</span><span class="p">]</span>  <span class="c"># /usr/bin:/usr/local/bin:/usr/X11/bin</span>
    <span class="n">paths</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">pathsep</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">program</span> <span class="ow">in</span> <span class="n">programs</span><span class="p">:</span>
        <span class="n">found</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span> <span class="c"># skip non-existing directories</span>
                <span class="c"># check read and execute access in this directory:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">R_OK</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">X_OK</span><span class="p">):</span>
                    <span class="k">print</span> <span class="nb">dir</span><span class="p">,</span> <span class="s">&#39;does not have read/execute access&#39;</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">program</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                    <span class="n">found</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">where</span><span class="p">:</span>
                        <span class="k">print</span> <span class="n">program</span><span class="p">,</span> <span class="s">&#39;found in&#39;</span><span class="p">,</span> <span class="nb">dir</span>
                    <span class="k">break</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;The program &#39;</span><span class="si">%s</span><span class="s">&#39; was not found&quot;</span> <span class="o">%</span> <span class="n">program</span>
            <span class="k">print</span> <span class="s">&#39;PATH contains the directories</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span>

    <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s">&#39;PYTHONPATH&#39;</span><span class="p">]</span>
    <span class="n">paths</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">pathsep</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">module</span> <span class="ow">in</span> <span class="n">modules</span><span class="p">:</span>
        <span class="n">found</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">for</span> <span class="nb">dir</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="nb">dir</span><span class="p">):</span> <span class="c"># skip non-existing directories</span>
                <span class="c"># check read and execute access in this directory:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">access</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">R_OK</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">X_OK</span><span class="p">):</span>
                    <span class="k">print</span> <span class="nb">dir</span><span class="p">,</span> <span class="s">&#39;does not have read/execute access&#39;</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">dir</span><span class="p">,</span><span class="n">module</span><span class="p">)</span> <span class="o">+</span> <span class="s">&#39;.py&#39;</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">fullpath</span><span class="p">):</span>
                    <span class="n">found</span> <span class="o">=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">where</span><span class="p">:</span>
                        <span class="k">print</span> <span class="n">module</span><span class="p">,</span> <span class="s">&#39;found in&#39;</span><span class="p">,</span> <span class="nb">dir</span>
                    <span class="k">break</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">found</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;The module &#39;</span><span class="si">%s</span><span class="s">&#39; was not found&quot;</span> <span class="o">%</span> <span class="n">module</span>
            <span class="k">print</span> <span class="s">&#39;PYTHONPATH contains the directories</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span>\
            <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="preprocess_all_files"><a class="viewcode-back" href="../../misc.html#scitools.misc.preprocess_all_files">[docs]</a><span class="k">def</span> <span class="nf">preprocess_all_files</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">options</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run preprocess on all files of the form basename.p.ext</span>
<span class="sd">    in the directory with root rootdir. The output of each</span>
<span class="sd">    preprocess run is directed to basename.ext.</span>

<span class="sd">    @param rootdir: root of directory tree to be processed.</span>
<span class="sd">    @param options: options (string) to preprocess program.</span>
<span class="sd">    @return: nested list of ((dir, basename.p.ext, basename.p), success))</span>
<span class="sd">    tuples. success is boolean and indicates if the preprocess command</span>
<span class="sd">    was a success (or not).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># first check that the user has the preprocess script:</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">findprograms</span><span class="p">(</span><span class="s">&#39;preprocess&#39;</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">SystemError</span><span class="p">(</span><span class="s">&#39;The preprocess program could not be found&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">treat_a_dir</span><span class="p">(</span><span class="n">fileinfo</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">files</span><span class="p">):</span>
        <span class="n">warning</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="s">#############################################################################</span>
<span class="s"># WARNING: This is an autogenerated file!! Do not edit this file!!</span>
<span class="s"># Edit the original file </span><span class="si">%s</span><span class="s"> (on which preprocess.py will be run)</span>
<span class="s">#############################################################################</span>
<span class="s">&quot;&quot;&quot;</span>

        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
            <span class="k">if</span> <span class="s">&#39;.p.&#39;</span> <span class="ow">in</span> <span class="n">f</span> <span class="ow">and</span> <span class="ow">not</span> <span class="s">&#39;.svn&#39;</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
                <span class="n">basename_dotp</span><span class="p">,</span> <span class="n">ext</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
                <span class="n">basename</span><span class="p">,</span> <span class="n">dotp</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">basename_dotp</span><span class="p">)</span>
                <span class="n">outfilename</span> <span class="o">=</span> <span class="n">basename</span> <span class="o">+</span> <span class="n">ext</span>
                <span class="n">outpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">outfilename</span><span class="p">)</span>
                <span class="n">cmd</span> <span class="o">=</span> <span class="s">&#39;preprocess </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s"> &gt; </span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">outpath</span><span class="p">)</span>
                <span class="c">#print cmd</span>
                <span class="n">failure</span><span class="p">,</span> <span class="n">output</span> <span class="o">=</span> <span class="n">system</span><span class="p">(</span><span class="n">cmd</span><span class="p">,</span> <span class="n">failure_handling</span><span class="o">=</span><span class="s">&#39;warning&#39;</span><span class="p">)</span>
                <span class="n">fileinfo</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="p">((</span><span class="n">d</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">outfilename</span><span class="p">),</span> <span class="ow">not</span> <span class="n">failure</span><span class="p">))</span>
                <span class="c"># add warning header:</span>
                <span class="n">_warning</span> <span class="o">=</span> <span class="n">warning</span> <span class="o">%</span> <span class="n">f</span>
                <span class="n">_f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outpath</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">);</span> <span class="n">_str</span> <span class="o">=</span> <span class="n">_f</span><span class="o">.</span><span class="n">read</span><span class="p">();</span> <span class="n">_f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="n">_lines</span> <span class="o">=</span> <span class="n">_str</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;#!&#39;</span><span class="p">):</span>
                    <span class="n">_lines</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">_warning</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">_lines</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">_warning</span><span class="p">)</span>
                <span class="n">_str</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">_lines</span><span class="p">)</span>
                <span class="n">_f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outpath</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">);</span> <span class="n">_f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">_str</span><span class="p">);</span> <span class="n">_f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="n">info</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">rootdir</span><span class="p">,</span> <span class="n">treat_a_dir</span><span class="p">,</span> <span class="n">info</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">info</span>

</div>
<div class="viewcode-block" id="pow_eff"><a class="viewcode-back" href="../../misc.html#scitools.misc.pow_eff">[docs]</a><span class="k">def</span> <span class="nf">pow_eff</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span> <span class="n">powfunc</span><span class="o">=</span><span class="n">math</span><span class="o">.</span><span class="n">pow</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns a^b. Smart function that happened to be slower</span>
<span class="sd">    than a straight math.pow.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">a</span>
    <span class="k">elif</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">3</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="n">a</span>
    <span class="k">elif</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
        <span class="n">h</span> <span class="o">=</span> <span class="n">a</span><span class="o">*</span><span class="n">a</span>
        <span class="k">return</span> <span class="n">h</span><span class="o">*</span><span class="n">h</span>
    <span class="k">elif</span> <span class="n">b</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">a</span>
    <span class="k">elif</span> <span class="nb">abs</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mf">1.0E-15</span><span class="p">:</span>  <span class="c"># x^0 ?</span>
        <span class="k">return</span> <span class="mf">1.0</span>
    <span class="k">elif</span> <span class="n">a</span> <span class="o">==</span> <span class="mf">0.0</span><span class="p">:</span>
        <span class="k">return</span> <span class="mf">0.0</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># check if b is integer:</span>
        <span class="n">bi</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bi</span> <span class="o">==</span> <span class="n">b</span><span class="p">:</span>
            <span class="n">r</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bi</span><span class="p">):</span>
                <span class="n">r</span> <span class="o">*=</span> <span class="n">a</span>
            <span class="k">return</span> <span class="n">r</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&#39;pow(a,b) with a=</span><span class="si">%g</span><span class="s">&lt;0&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">powfunc</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="lines2paragraphs"><a class="viewcode-back" href="../../misc.html#scitools.misc.lines2paragraphs">[docs]</a><span class="k">def</span> <span class="nf">lines2paragraphs</span><span class="p">(</span><span class="n">lines</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a list of paragraphs from a list of lines</span>
<span class="sd">    (normally holding the lines in a file).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="p">[]</span>             <span class="c"># list of paragraphs to be returned</span>
    <span class="n">firstline</span> <span class="o">=</span> <span class="mi">0</span>      <span class="c"># first line in a paragraph</span>
    <span class="n">currentline</span> <span class="o">=</span> <span class="mi">0</span>    <span class="c"># current line in the file</span>
    <span class="n">lines</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">),</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span> <span class="c"># needed to get the last paragraph</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
        <span class="c"># for each new blank line, join lines from firstline</span>
        <span class="c"># to currentline to a string defining a new paragraph:</span>
        <span class="c">#if re.search(r&#39;^\s*$&#39;, line):  # blank line?</span>
        <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">isspace</span><span class="p">():</span>  <span class="c"># blank line?</span>
            <span class="k">if</span> <span class="n">currentline</span> <span class="o">&gt;</span> <span class="n">firstline</span><span class="p">:</span>
                <span class="n">p</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">firstline</span><span class="p">:</span><span class="n">currentline</span><span class="o">+</span><span class="mi">1</span><span class="p">]))</span>
                <span class="c">#print &#39;paragraph from line&#39;,firstline,&#39;to&#39;,currentline</span>
            <span class="c"># new paragraph starts from the next line:</span>
            <span class="n">firstline</span> <span class="o">=</span> <span class="n">currentline</span><span class="o">+</span><span class="mi">1</span>
        <span class="n">currentline</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">p</span>

</div>
<div class="viewcode-block" id="oneline"><a class="viewcode-back" href="../../misc.html#scitools.misc.oneline">[docs]</a><span class="k">def</span> <span class="nf">oneline</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">outfile</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Transform all paragraphs in infile (filename) to one-line strings</span>
<span class="sd">    and write the result to outfile (filename).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fi</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">pp</span> <span class="o">=</span> <span class="n">lines2paragraphs</span><span class="p">(</span><span class="n">fi</span><span class="o">.</span><span class="n">readlines</span><span class="p">())</span>
    <span class="n">fo</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pp</span><span class="p">:</span>
        <span class="n">line</span> <span class="o">=</span> <span class="s">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">))</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n\n</span><span class="s">&#39;</span>
        <span class="n">fo</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
    <span class="n">fi</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">fo</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="wrap"><a class="viewcode-back" href="../../misc.html#scitools.misc.wrap">[docs]</a><span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">linewidth</span><span class="o">=</span><span class="mi">70</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read infile (filename) and format the text such that each line is</span>
<span class="sd">    not longer than linewidth. Write result to outfile (filename).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">fi</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">fo</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
    <span class="c"># the use of textwrap must be done paragraph by paragraph:</span>
    <span class="kn">from</span> <span class="nn">textwrap</span> <span class="kn">import</span> <span class="n">wrap</span>
    <span class="n">pp</span> <span class="o">=</span> <span class="n">lines2paragraphs</span><span class="p">(</span><span class="n">fi</span><span class="o">.</span><span class="n">readlines</span><span class="p">())</span>
    <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">pp</span><span class="p">:</span>
        <span class="c">#print &#39;paragraph:\n  &quot;%s&quot;&#39; % p</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="n">wrap</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">linewidth</span><span class="p">)</span>
        <span class="c">#print &#39;lines:\n&#39;, lines</span>
        <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">:</span>
            <span class="n">fo</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span> <span class="o">+</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
        <span class="n">fo</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
    <span class="n">fi</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="n">fo</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="fontscheme1"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme1">[docs]</a><span class="k">def</span> <span class="nf">fontscheme1</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">pulldown_font</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="s">&#39;italic bold&#39;</span><span class="p">)</span>
    <span class="n">scale_font</span>    <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Menu*Font&#39;</span><span class="p">,</span> <span class="n">pulldown_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Menubutton*Font&#39;</span><span class="p">,</span> <span class="n">pulldown_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Scale.*Font&#39;</span><span class="p">,</span> <span class="n">scale_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme2"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme2">[docs]</a><span class="k">def</span> <span class="nf">fontscheme2</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">pulldown_font</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="s">&#39;italic bold&#39;</span><span class="p">)</span>
    <span class="n">scale_font</span>    <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Helvetica&#39;</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Menu*Font&#39;</span><span class="p">,</span> <span class="n">pulldown_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Menubutton*Font&#39;</span><span class="p">,</span> <span class="n">pulldown_font</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Scale.*Font&#39;</span><span class="p">,</span> <span class="n">scale_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme3"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme3">[docs]</a><span class="k">def</span> <span class="nf">fontscheme3</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Fixed&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme4"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme4">[docs]</a><span class="k">def</span> <span class="nf">fontscheme4</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;Fixed&#39;</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme5"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme5">[docs]</a><span class="k">def</span> <span class="nf">fontscheme5</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;comic sans ms&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme6"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme6">[docs]</a><span class="k">def</span> <span class="nf">fontscheme6</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;trebuchet ms&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="s">&#39;normal bold&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme7"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme7">[docs]</a><span class="k">def</span> <span class="nf">fontscheme7</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;verdana&#39;</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="s">&#39;normal bold&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="fontscheme8"><a class="viewcode-back" href="../../misc.html#scitools.misc.fontscheme8">[docs]</a><span class="k">def</span> <span class="nf">fontscheme8</span><span class="p">(</span><span class="n">root</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Alternative font scheme for Tkinter-based widgets.&quot;&quot;&quot;</span>
    <span class="n">default_font</span>  <span class="o">=</span> <span class="p">(</span><span class="s">&#39;verdana&#39;</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="s">&#39;normal&#39;</span><span class="p">)</span>
    <span class="n">root</span><span class="o">.</span><span class="n">option_add</span><span class="p">(</span><span class="s">&#39;*Font&#39;</span><span class="p">,</span> <span class="n">default_font</span><span class="p">)</span>



</div>
<div class="viewcode-block" id="movefiles"><a class="viewcode-back" href="../../misc.html#scitools.misc.movefiles">[docs]</a><span class="k">def</span> <span class="nf">movefiles</span><span class="p">(</span><span class="n">files</span><span class="p">,</span> <span class="n">destdir</span><span class="p">,</span> <span class="n">confirm</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">copy</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Move a set of files to a a destination directory tree,</span>
<span class="sd">    but let the original complete path be reflected in the</span>
<span class="sd">    destination tree.</span>

<span class="sd">    files         list of filenames</span>
<span class="sd">    destdir       root of destination directory tree</span>
<span class="sd">    confirm       let the user confirm movement of each file</span>
<span class="sd">    verbose       write out the original and new path of each file</span>
<span class="sd">    copy          True: copy, False: move</span>

<span class="sd">    The function is useful for backing up or temporarily moving</span>
<span class="sd">    files; the files are easily restored in their original</span>
<span class="sd">    locations since the original complete path is maintained in</span>
<span class="sd">    the destination directory tree.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">destdir</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">destdir</span><span class="p">)</span>
    <span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
        <span class="n">perform_action</span> <span class="o">=</span> <span class="s">&#39;y&#39;</span>
        <span class="k">if</span> <span class="n">confirm</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;move </span><span class="si">%s</span><span class="s"> to </span><span class="si">%s</span><span class="s">?&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">destdir</span><span class="p">)</span>
            <span class="n">perform_action</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">perform_action</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">,</span> <span class="s">&#39;Y&#39;</span><span class="p">,</span> <span class="s">&#39;yes&#39;</span><span class="p">,</span> <span class="s">&#39;YES&#39;</span><span class="p">):</span>
            <span class="n">fullpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="nb">file</span><span class="p">)</span>
            <span class="c"># remove initial / (Unix) or C:\ (Windows):</span>
            <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&#39;win&#39;</span><span class="p">):</span>
                <span class="n">fullpath</span> <span class="o">=</span> <span class="n">fullpath</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>
            <span class="k">else</span><span class="p">:</span>  <span class="c"># Unix</span>
                <span class="n">fullpath</span> <span class="o">=</span> <span class="n">fullpath</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
            <span class="n">newpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">destdir</span><span class="p">,</span> <span class="n">fullpath</span><span class="p">)</span>
            <span class="n">newdir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">newpath</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">newdir</span><span class="p">):</span> <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">newdir</span><span class="p">)</span>
            <span class="n">shutil</span><span class="o">.</span><span class="n">copy2</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="n">newpath</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">newpath</span><span class="p">):</span>
                <span class="k">print</span> <span class="s">&#39;fount&#39;</span><span class="p">,</span><span class="n">newpath</span>
            <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;copied&#39;</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">copy</span><span class="p">:</span>  <span class="c"># pure move</span>
                <span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="nb">file</span><span class="p">);</span> <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;moved&#39;</span>
            <span class="k">if</span> <span class="n">verbose</span><span class="p">:</span>
                <span class="k">print</span> <span class="n">s</span><span class="p">,</span> <span class="nb">file</span><span class="p">,</span> <span class="s">&#39;to&#39;</span><span class="p">,</span> <span class="n">newpath</span>

<span class="c"># backward compatibility:</span></div>
<span class="kn">from</span> <span class="nn">debug</span> <span class="kn">import</span> <span class="n">debugregex</span><span class="p">,</span> <span class="n">dump</span>



<div class="viewcode-block" id="BackgroundCommand"><a class="viewcode-back" href="../../misc.html#scitools.misc.BackgroundCommand">[docs]</a><span class="k">class</span> <span class="nc">BackgroundCommand</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Run a function call with assignment in the background.</span>
<span class="sd">    Useful for putting time-consuming calculations/graphics</span>
<span class="sd">    in the background in an interactive Python shell.</span>

<span class="sd">    &gt;&gt;&gt; b=BG(&#39;f&#39;, g.gridloop, &#39;sin(x*y)-exp(-x*y)&#39;)</span>
<span class="sd">    &gt;&gt;&gt; b.start()</span>
<span class="sd">    running gridloop(&#39;sin(x*y)-exp(-x*y)&#39;,) in a thread</span>
<span class="sd">    &gt;&gt;&gt; # continue with other interactive tasks</span>
<span class="sd">    &gt;&gt;&gt; b.finished</span>
<span class="sd">    True</span>
<span class="sd">    &gt;&gt;&gt; b.f  # result of function call</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="o">=</span><span class="s">&#39;result&#39;</span><span class="p">,</span> <span class="n">func</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">[],</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{}):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">result</span> <span class="o">=</span> <span class="n">result</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">result</span><span class="p">]</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">args</span> <span class="o">=</span> <span class="n">args</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span> <span class="o">=</span> <span class="n">kwargs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finished</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<div class="viewcode-block" id="BackgroundCommand.run"><a class="viewcode-back" href="../../misc.html#scitools.misc.BackgroundCommand.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">kw</span> <span class="o">=</span> <span class="p">[</span><span class="n">key</span><span class="o">+</span><span class="s">&#39;=&#39;</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">]</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">(</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">result</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span>
                                <span class="s">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">),</span> <span class="s">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">kw</span><span class="p">))</span>
        <span class="k">print</span> <span class="s">&#39;running </span><span class="si">%s</span><span class="s"> in a thread&#39;</span> <span class="o">%</span> <span class="n">cmd</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">result</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">,</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finished</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">print</span> <span class="n">cmd</span><span class="p">,</span> <span class="s">&#39;finished&#39;</span>
</div></div>
<span class="n">BG</span> <span class="o">=</span> <span class="n">BackgroundCommand</span>  <span class="c"># short form</span>

<div class="viewcode-block" id="Download"><a class="viewcode-back" href="../../misc.html#scitools.misc.Download">[docs]</a><span class="k">class</span> <span class="nc">Download</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="n">url</span><span class="p">;</span>  <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">filename</span>
        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<div class="viewcode-block" id="Download.run"><a class="viewcode-back" href="../../misc.html#scitools.misc.Download.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">print</span> <span class="s">&#39;Fetching&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">url</span>
        <span class="n">urllib</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">)</span>
        <span class="k">print</span> <span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="p">,</span> <span class="s">&#39;is downloaded&#39;</span>
</div></div>
<div class="viewcode-block" id="hardware_info"><a class="viewcode-back" href="../../misc.html#scitools.misc.hardware_info">[docs]</a><span class="k">def</span> <span class="nf">hardware_info</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a dictionary of various types of hardware info from</span>
<span class="sd">    various modules.</span>

<span class="sd">    Recommended use::</span>

<span class="sd">      from scitools.misc import hardware_info</span>
<span class="sd">      import pprint; pprint.pprint(hardware_info())</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="c"># read cpuinfo file on Linux machines</span>
    <span class="c"># (numpy.distutils.cpuinfo already does this)</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    infofile = &#39;/proc/cpuinfo&#39;</span>
<span class="sd">    cpuinfo = {}</span>
<span class="sd">    if os.path.isfile(infofile):</span>
<span class="sd">        f = open(infofile, &#39;r&#39;)</span>
<span class="sd">        for line in f:</span>
<span class="sd">            try:</span>
<span class="sd">                name, value = [w.strip() for w in line.split(&#39;:&#39;)]</span>
<span class="sd">            except:</span>
<span class="sd">                continue</span>
<span class="sd">            if name not in (&#39;model&#39;, &#39;processor&#39;, &#39;stepping&#39;, &#39;flags&#39;,):</span>
<span class="sd">                cpuinfo[name] = value</span>
<span class="sd">            # Override a few with better names</span>
<span class="sd">            if name == &#39;model name&#39;:</span>
<span class="sd">                cpuinfo[&#39;CPU type&#39;] = value</span>
<span class="sd">            elif name == &#39;cache size&#39;:</span>
<span class="sd">                cpuinfo[&#39;cache size&#39;] = value</span>
<span class="sd">            elif name == &#39;cpu MHz&#39;:</span>
<span class="sd">                cpuinfo[&#39;CPU speed&#39;] = value + &#39; Hz&#39;</span>
<span class="sd">            elif name == &#39;vendor_id&#39;:</span>
<span class="sd">                cpuinfo[&#39;vendor ID&#39;] = value</span>
<span class="sd">        f.close()</span>
<span class="sd">    result[&#39;file: /proc/cpuinfo&#39;] = cpuinfo</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">numpy.distutils.cpuinfo</span><span class="o">,</span> <span class="nn">copy</span>
    <span class="n">info</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">distutils</span><span class="o">.</span><span class="n">cpuinfo</span><span class="o">.</span><span class="n">cpu</span><span class="o">.</span><span class="n">info</span><span class="p">)</span>
    <span class="c"># Delete some of the items</span>
    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">(</span><span class="s">&#39;model&#39;</span><span class="p">,</span> <span class="s">&#39;processor&#39;</span><span class="p">,</span> <span class="s">&#39;stepping&#39;</span><span class="p">,</span> <span class="s">&#39;flags&#39;</span><span class="p">,):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">info</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">info</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
    <span class="n">result</span><span class="p">[</span><span class="s">&#39;numpy.distutils.cpuinfo.cpu.info&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">info</span>

    <span class="c"># check out platform module from basic Python:</span>
    <span class="kn">import</span> <span class="nn">platform</span>
    <span class="n">platform_info</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">&#39;uname&#39;</span><span class="p">:</span> <span class="n">platform</span><span class="o">.</span><span class="n">uname</span><span class="p">(),</span>
        <span class="s">&#39;python version&#39;</span><span class="p">:</span> <span class="n">platform</span><span class="o">.</span><span class="n">python_version</span><span class="p">(),</span>
        <span class="s">&#39;python build&#39;</span><span class="p">:</span> <span class="n">platform</span><span class="o">.</span><span class="n">python_build</span><span class="p">(),</span>
        <span class="s">&#39;identifier&#39;</span><span class="p">:</span> <span class="n">platform</span><span class="o">.</span><span class="n">platform</span><span class="p">()}</span>
    <span class="n">result</span><span class="p">[</span><span class="s">&#39;platform module&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">platform_info</span>

    <span class="c"># Trent Mick&#39;s platinfo module</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">platinfo2</span>  <span class="c"># code.google.com/p/platinfo</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">pi</span> <span class="o">=</span> <span class="n">platinfo</span><span class="o">.</span><span class="n">PlatInfo</span><span class="p">()</span>
            <span class="n">result</span><span class="p">[</span><span class="s">&#39;platinfo2 module&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pi</span><span class="o">.</span><span class="n">as_dict</span><span class="p">()</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">pass</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">pass</span>

    <span class="k">return</span> <span class="n">result</span>

</div>
<div class="viewcode-block" id="memusage"><a class="viewcode-back" href="../../misc.html#scitools.misc.memusage">[docs]</a><span class="k">def</span> <span class="nf">memusage</span><span class="p">(</span><span class="n">_proc_pid_stat</span> <span class="o">=</span> <span class="s">&#39;/proc/</span><span class="si">%s</span><span class="s">/stat&#39;</span><span class="o">%</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getpid</span><span class="p">())):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return virtual memory size in bytes of the running python.</span>
<span class="sd">    Copied from the SciPy package (scipy_test.testing.py).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">f</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="n">_proc_pid_stat</span><span class="p">,</span><span class="s">&#39;r&#39;</span><span class="p">)</span>
        <span class="n">l</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39; &#39;</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="mi">22</span><span class="p">])</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="k">return</span>

</div>
<span class="k">def</span> <span class="nf">_test_memusage</span><span class="p">(</span><span class="n">narrays</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">m</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Test the memusage function for monitoring the memory usage.</span>
<span class="sd">    Generate narrays arrays of size (m,m). Keep the array</span>
<span class="sd">    with probability 0.5, otherwise delete a previously</span>
<span class="sd">    allocated array.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">random</span><span class="o">,</span> <span class="nn">Numeric</span>
    <span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">12</span><span class="p">)</span>
    <span class="n">refs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">narrays</span><span class="p">):</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">Numeric</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">m</span><span class="p">,</span><span class="n">m</span><span class="p">),</span> <span class="n">Numeric</span><span class="o">.</span><span class="n">Float</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&gt;</span> <span class="mf">0.5</span><span class="p">:</span>
            <span class="n">refs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">refs</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">refs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">memusage</span><span class="p">()</span><span class="o">/</span><span class="mf">1000000.0</span>
        <span class="k">print</span> <span class="s">&#39;memory usage: </span><span class="si">%.2f</span><span class="s">Mb&#39;</span> <span class="o">%</span> <span class="n">mu</span>


<div class="viewcode-block" id="isiterable"><a class="viewcode-back" href="../../misc.html#scitools.misc.isiterable">[docs]</a><span class="k">def</span> <span class="nf">isiterable</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns true of data is iterable, else False.&quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="nb">iter</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="bp">True</span>
</div>
<div class="viewcode-block" id="flatten"><a class="viewcode-back" href="../../misc.html#scitools.misc.flatten">[docs]</a><span class="k">def</span> <span class="nf">flatten</span><span class="p">(</span><span class="n">nested_data</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a flattened iterator over nested_data.</span>

<span class="sd">    &gt;&gt;&gt; nested_list = [[1,2],3,[4,5,6,[7,[8,9]]]]</span>
<span class="sd">    &gt;&gt;&gt; flat = [e for e in flatten(nested_list)]</span>
<span class="sd">    &gt;&gt;&gt; flat</span>
<span class="sd">    [1, 2, 3, 4, 5, 6, 7, 8, 9]</span>

<span class="sd">    (Minor adjustment of code by Goncalo Rodrigues, see</span>
<span class="sd">    http://aspn.activestate.com/ASPN/Mail/Message/python-tutor/2302348)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">nested_data</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
        <span class="c"># note: strings are bad because, when iterated they return</span>
        <span class="c"># strings, leading to an infinite loop</span>
        <span class="k">if</span> <span class="n">isiterable</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="nb">basestring</span><span class="p">):</span>
            <span class="c"># recurse into iterators</span>
            <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">flatten</span><span class="p">(</span><span class="n">e</span><span class="p">):</span>
                <span class="k">yield</span> <span class="n">f</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">e</span>
</div>
<div class="viewcode-block" id="primes"><a class="viewcode-back" href="../../misc.html#scitools.misc.primes">[docs]</a><span class="k">def</span> <span class="nf">primes</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the prime numbers &lt;= n.</span>
<span class="sd">    Standard optimized sieve algorithm.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>  <span class="k">return</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span> <span class="k">return</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
    <span class="c"># do only odd numbers starting at 3</span>
    <span class="n">s</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">mroot</span> <span class="o">=</span> <span class="n">n</span><span class="o">**</span><span class="mf">0.5</span>
    <span class="n">half</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">m</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="k">while</span> <span class="n">m</span> <span class="o">&lt;=</span> <span class="n">mroot</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">s</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
            <span class="n">j</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">*</span><span class="n">m</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span><span class="o">//</span><span class="mi">2</span>  <span class="c"># int div</span>
            <span class="n">s</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">half</span><span class="p">:</span>
                <span class="n">s</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">j</span> <span class="o">+=</span> <span class="n">m</span>
        <span class="n">i</span> <span class="o">=</span> <span class="n">i</span><span class="o">+</span><span class="mi">1</span>
        <span class="n">m</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">i</span><span class="o">+</span><span class="mi">3</span>
    <span class="k">return</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">s</span> <span class="k">if</span> <span class="n">x</span><span class="p">]</span>

</div>
<div class="viewcode-block" id="cmldict"><a class="viewcode-back" href="../../misc.html#scitools.misc.cmldict">[docs]</a><span class="k">def</span> <span class="nf">cmldict</span><span class="p">(</span><span class="n">argv</span><span class="p">,</span> <span class="n">cmlargs</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">validity</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The cmldict function takes a dictionary cmlargs with default</span>
<span class="sd">    values for the command-line options and returns a modified form of</span>
<span class="sd">    this dictionary after the options given in the list argv are</span>
<span class="sd">    parsed and inserted. One will typically supply sys.argv[1:] as the</span>
<span class="sd">    argv argument. In case cmlargs is None, the dictionary is built</span>
<span class="sd">    from scratch inside the function.  The flag validity is false (0)</span>
<span class="sd">    if any option in argv can be inserted in cmlargs, otherwise the</span>
<span class="sd">    function will issue an error message if an option is not already</span>
<span class="sd">    present in cmlargs with a default value (notice that cmlargs=None</span>
<span class="sd">    and validity=1 is an incompatible setting).</span>

<span class="sd">    Example:</span>
<span class="sd">    cmlargs = {&#39;p&#39; : 0, &#39;file&#39; : None, &#39;q&#39; : 0, &#39;v&#39; : 0}</span>
<span class="sd">    argv = &quot;-p 2 --file out -q 0&quot;.split()</span>
<span class="sd">    p = cmldict(argv, cmlargs)</span>

<span class="sd">    p equals {&#39;p&#39; : 2, &#39;file&#39; : out, &#39;q&#39; : 0}</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">cmlargs</span><span class="p">:</span>
        <span class="n">cmlargs</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="n">arg_counter</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">while</span> <span class="n">arg_counter</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">argv</span><span class="p">):</span>
        <span class="n">option</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="n">arg_counter</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">option</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;-&#39;</span><span class="p">:</span>  <span class="n">option</span> <span class="o">=</span> <span class="n">option</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>  <span class="c"># remove 1st hyphen</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># not an option, proceed with next sys.argv entry</span>
            <span class="n">arg_counter</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span> <span class="k">continue</span>
        <span class="k">if</span> <span class="n">option</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;-&#39;</span><span class="p">:</span>  <span class="n">option</span> <span class="o">=</span> <span class="n">option</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>  <span class="c"># remove 2nd hyphen</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">validity</span> <span class="ow">or</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">cmlargs</span><span class="p">:</span>
            <span class="c"># next argv entry is the value:</span>
            <span class="n">arg_counter</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="n">arg_counter</span><span class="p">]</span>
            <span class="n">cmlargs</span><span class="p">[</span><span class="n">option</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">validity</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;The option </span><span class="si">%s</span><span class="s"> is not registered&quot;</span> <span class="o">%</span> <span class="n">option</span><span class="p">)</span>
        <span class="n">arg_counter</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">cmlargs</span>
</div>
<span class="k">def</span> <span class="nf">_cmldict_demo</span><span class="p">():</span>
    <span class="n">args</span> <span class="o">=</span> <span class="s">&quot;--m 9.1 --b 7 --c 0.1 -A 3.3&quot;</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&#39;m&#39;</span> <span class="p">:</span> <span class="s">&#39;1.8&#39;</span><span class="p">,</span> <span class="s">&#39;func&#39;</span> <span class="p">:</span> <span class="s">&#39;siny&#39;</span> <span class="p">}</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">cmldict</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">defaults</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="k">print</span> <span class="n">p</span>

    <span class="c"># shuffle values into other variables:</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="s">&#39;m&#39;</span><span class="p">]</span>
    <span class="n">b</span> <span class="o">=</span> <span class="n">p</span><span class="p">[</span><span class="s">&#39;b&#39;</span><span class="p">]</span>
    <span class="c"># and so on (should have validity=1 to ensure that the</span>
    <span class="c"># option keys are defined)</span>

    <span class="c"># take action:</span>
    <span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">p</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">option</span> <span class="o">==</span> <span class="s">&quot;m&quot;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;option is m&quot;</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">option</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">option</span> <span class="o">==</span> <span class="s">&quot;b&quot;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;option is b&quot;</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">option</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">option</span> <span class="o">==</span> <span class="s">&quot;c&quot;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;option is c&quot;</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">option</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">option</span> <span class="o">==</span> <span class="s">&quot;A&quot;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;option is A&quot;</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">option</span><span class="p">]</span>
        <span class="k">elif</span> <span class="n">option</span> <span class="o">==</span> <span class="s">&quot;func&quot;</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;option is func&quot;</span><span class="p">,</span> <span class="n">p</span><span class="p">[</span><span class="n">option</span><span class="p">]</span>

    <span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;--error&#39;</span><span class="p">);</span> <span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;yes&#39;</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">Now comes an exception (!)&quot;</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">cmldict</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">defaults</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="c"># used in StringFunction doc as an example:</span>
<span class="k">def</span> <span class="nf">_test_function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">c</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">a</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="o">-</span><span class="n">a</span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span>

<span class="c"># -- tests ---</span>
<div class="viewcode-block" id="f"><a class="viewcode-back" href="../../misc.html#scitools.misc.f">[docs]</a><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="nb">max</span><span class="o">=</span><span class="mf">1.2</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mf">2.2</span><span class="p">):</span>  <span class="c"># some function</span>
    <span class="k">print</span> <span class="s">&#39;a=</span><span class="si">%g</span><span class="s">, b=</span><span class="si">%g</span><span class="s">, max=</span><span class="si">%g</span><span class="s">, min=</span><span class="si">%g</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="nb">max</span><span class="p">,</span><span class="nb">min</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="DoNothing"><a class="viewcode-back" href="../../misc.html#scitools.misc.DoNothing">[docs]</a><span class="k">class</span> <span class="nc">DoNothing</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Handy class for making other objects inactive.</span>
<span class="sd">    (DoNothing is a generic dispatcher, accepting anyting and</span>
<span class="sd">    doing nothing.)</span>

<span class="sd">    Whatever we do, we always get a DoNothing object, with which</span>
<span class="sd">    we can do whatever we want to, but nothing will happen.</span>

<span class="sd">    For example, say a plot function returns a plot object that</span>
<span class="sd">    is used widely in a code to create windows with visualizations</span>
<span class="sd">    on the screen, and you want to turn off all these visualizations:</span>

<span class="sd">    &gt;&gt;&gt; from scitools.misc import DoNothing</span>
<span class="sd">    &gt;&gt;&gt; plot = DoNothing(&#39;Plotting turned off&#39;)</span>
<span class="sd">    &gt;&gt;&gt; viz = plot(u, wireframe=True, title=&#39;My plot&#39;)</span>
<span class="sd">    &gt;&gt;&gt; type(viz)</span>
<span class="sd">    &lt;class &#39;scitools.misc.DoNothing&#39;&gt;</span>
<span class="sd">    &gt;&gt;&gt; viz.update(T)</span>
<span class="sd">    trying update but no action (DoNothing object)</span>
<span class="sd">    &gt;&gt;&gt; q = viz.properties()</span>
<span class="sd">    trying properties but no action (DoNothing object)</span>
<span class="sd">    &gt;&gt;&gt; type(q)</span>
<span class="sd">    &lt;class &#39;scitools.misc.DoNothing&#39;&gt;</span>

<span class="sd">    One can turn the messages &#39;trying ... but no action&#39; off by</span>
<span class="sd">    giving the argument silent=True to the constructor:</span>

<span class="sd">    &gt;&gt;&gt; plot = DoNothing(&#39;Plotting turned off&#39;, silent=True)</span>
<span class="sd">    &gt;&gt;&gt; viz = plot(u, wireframe=True, title=&#39;My plot&#39;)</span>
<span class="sd">    &gt;&gt;&gt; viz.update(T)</span>
<span class="sd">    &gt;&gt;&gt; q = viz.properties()</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">silent</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;silent&#39;</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">DoNothing</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getattribute__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="o">!=</span> <span class="s">&#39;silent&#39;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">silent</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&#39;ignoring action &quot;</span><span class="si">%s</span><span class="s">&quot; (DoNothing object)&#39;</span> <span class="o">%</span> <span class="n">name</span>
        <span class="k">return</span> <span class="n">DoNothing</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span>

<div class="viewcode-block" id="DoNothing.next"><a class="viewcode-back" href="../../misc.html#scitools.misc.DoNothing.next">[docs]</a>    <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">StopIteration</span><span class="p">()</span>

</div></div>
<div class="viewcode-block" id="Recorder"><a class="viewcode-back" href="../../misc.html#scitools.misc.Recorder">[docs]</a><span class="k">class</span> <span class="nc">Recorder</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class is a wrapper of a module or instance which will</span>
<span class="sd">    record all actions done with the module/instance.</span>

<span class="sd">    &gt;&gt;&gt; from scitools.misc import Recorder</span>
<span class="sd">    &gt;&gt;&gt; from scitools.std import plt, plot, linspace</span>
<span class="sd">    &gt;&gt;&gt; x = linspace(-1,1,10)</span>
<span class="sd">    &gt;&gt;&gt; y1 = x**2</span>
<span class="sd">    &gt;&gt;&gt; y2 = x**3</span>
<span class="sd">    &gt;&gt;&gt; plt._g = Recorder(plt._g)  # make the plot object record itself</span>
<span class="sd">    &gt;&gt;&gt; plot(x, y1, &#39;r-&#39;,</span>
<span class="sd">    ...      x, y2, &#39;b-&#39;,</span>
<span class="sd">    ...      title=&#39;A test&#39;)</span>
<span class="sd">    [&lt;scitools.easyviz.common.Line object at 0x1749c50&gt;, &lt;scitools.easyviz.common.Line object at 0x1749bd0&gt;]</span>
<span class="sd">    &gt;&gt;&gt; # look at what we have done with the plt._g object</span>
<span class="sd">    &gt;&gt;&gt; plt._g.replay()</span>
<span class="sd">    reset()</span>
<span class="sd">    __call__(&#39;unset multiplot&#39;,)</span>
<span class="sd">    __call__(&#39;set datafile missing &quot;nan&quot;&#39;,)</span>
<span class="sd">    __call__(&#39;set key right top&#39;,)</span>
<span class="sd">    __call__(&#39;set title &quot;A test&quot;&#39;,)</span>
<span class="sd">    __call__(&#39;unset logscale x&#39;,)</span>
<span class="sd">    __call__(&#39;unset logscale y&#39;,)</span>
<span class="sd">    __call__(&#39;set autoscale&#39;,)</span>
<span class="sd">    __call__(&#39;set xrange[*:*]&#39;,)</span>
<span class="sd">    __call__(&#39;set yrange[*:*]&#39;,)</span>
<span class="sd">    __call__(&#39;set zrange[*:*]&#39;,)</span>
<span class="sd">    __call__(&#39;set size noratio&#39;,)</span>
<span class="sd">    __call__(&#39;set size nosquare&#39;,)</span>
<span class="sd">    __call__(&#39;set yrange [] noreverse&#39;,)</span>
<span class="sd">    __call__(&#39;set hidden3d&#39;,)</span>
<span class="sd">    __call__(&#39;unset colorbox&#39;,)</span>
<span class="sd">    __call__(&#39;set cbrange [*:*]&#39;,)</span>
<span class="sd">    __call__(&#39;set palette model RGB defined (0 &quot;blue&quot;, 3 &quot;cyan&quot;, 4 &quot;green&quot;, 5 &quot;yellow&quot;, 8 &quot;red&quot;, 10 &quot;black&quot;)&#39;,)</span>
<span class="sd">    __call__(&#39;unset view&#39;,)</span>
<span class="sd">    __call__(&#39;set view map&#39;,)</span>
<span class="sd">    __call__(&#39;set xtics&#39;,)</span>
<span class="sd">    __call__(&#39;set ytics&#39;,)</span>
<span class="sd">    __call__(&#39;set ztics&#39;,)</span>
<span class="sd">    __call__(&#39;set border 1+2+4+8+16 linetype -1 linewidth .4&#39;,)</span>
<span class="sd">    __call__(&#39;unset xlabel&#39;,)</span>
<span class="sd">    __call__(&#39;unset ylabel&#39;,)</span>
<span class="sd">    __call__(&#39;unset zlabel&#39;,)</span>
<span class="sd">    __call__(&#39;set border 4095 linetype -1 linewidth .4&#39;,)</span>
<span class="sd">    __call__(&#39;unset grid&#39;,)</span>
<span class="sd">    plot(&lt;Gnuplot.PlotItems._FIFOFileItem instance at 0x174d998&gt;,)</span>
<span class="sd">    replot(&lt;Gnuplot.PlotItems._FIFOFileItem instance at 0x174db90&gt;,)</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obj</span> <span class="o">=</span> <span class="n">obj</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">recorder</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_RecordHelper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">recorder</span><span class="p">)</span>

<div class="viewcode-block" id="Recorder.replay"><a class="viewcode-back" href="../../misc.html#scitools.misc.Recorder.replay">[docs]</a>    <span class="k">def</span> <span class="nf">replay</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">recorder</span><span class="p">:</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&#39;(&#39;</span>
            <span class="k">if</span> <span class="n">args</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">+=</span> <span class="nb">str</span><span class="p">(</span><span class="n">args</span><span class="p">)[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">s</span> <span class="o">+=</span> <span class="s">&#39;, &#39;</span> <span class="o">+</span> <span class="s">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">=</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">[</span><span class="n">key</span><span class="p">])</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">])</span>
            <span class="n">s</span> <span class="o">+=</span> <span class="s">&#39;)&#39;</span>
            <span class="k">print</span> <span class="n">s</span>
</div></div>
<span class="k">class</span> <span class="nc">_RecordHelper</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">recorder</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">recorder</span> <span class="o">=</span> <span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">recorder</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">recorder</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s"> has no attribute </span><span class="si">%s</span><span class="s">&#39;</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>



<div class="viewcode-block" id="fix_latex_command_regex"><a class="viewcode-back" href="../../misc.html#scitools.misc.fix_latex_command_regex">[docs]</a><span class="k">def</span> <span class="nf">fix_latex_command_regex</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">application</span><span class="o">=</span><span class="s">&#39;match&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a pattern for a regular expression match or substitution,</span>
<span class="sd">    the function checks for problematic patterns commonly</span>
<span class="sd">    encountered when working with LaTeX texts, namely commands</span>
<span class="sd">    starting with a backslash.</span>

<span class="sd">    For a pattern to be matched or substituted, and extra backslash is</span>
<span class="sd">    always needed (either a special regex construction like \w leads</span>
<span class="sd">    to wrong match, or \c leads to wrong substitution since \ just</span>
<span class="sd">    escapes c so only the c is replaced, leaving an undesired</span>
<span class="sd">    backslash). For the replacement pattern in a substitutions, specified</span>
<span class="sd">    by the application=&#39;replacement&#39; argument, a backslash</span>
<span class="sd">    before any of the characters abfgnrtv must be preceeded by an</span>
<span class="sd">    additional backslash.</span>

<span class="sd">    The application variable equals &#39;match&#39; if pattern is used for</span>
<span class="sd">    a match and &#39;replacement&#39; if pattern defines a replacement</span>
<span class="sd">    regex in a re.sub command.</span>

<span class="sd">    Caveats: let pattern just contain LaTeX commands, not combination</span>
<span class="sd">    of commands and other regular expressions (\s, \d, etc.) as the</span>
<span class="sd">    latter will end up with an extra undesired backslash.</span>

<span class="sd">    Here are examples on failures:</span>

<span class="sd">    &gt;&gt;&gt; re.sub(r&#39;\begin\{equation\}&#39;, r&#39;\[&#39;, r&#39;\begin{equation}&#39;)</span>
<span class="sd">    &#39;\\begin{equation}&#39;</span>
<span class="sd">    &gt;&gt;&gt; # match of mbox, not \mbox, and wrong output:</span>
<span class="sd">    &gt;&gt;&gt; re.sub(r&#39;\mbox\{(.+?)\}&#39;, r&#39;\fbox{\g&lt;1&gt;}&#39;, r&#39;\mbox{not}&#39;)</span>
<span class="sd">    &#39;\\\x0cbox{not}&#39;</span>

<span class="sd">    Here are examples on using this function:</span>

<span class="sd">    &gt;&gt;&gt; from scitools.misc import fix_latex_command_regex as fix</span>
<span class="sd">    &gt;&gt;&gt; pattern = fix(r&#39;\begin\{equation\}&#39;, application=&#39;match&#39;)</span>
<span class="sd">    &gt;&gt;&gt; re.sub(pattern, r&#39;\[&#39;, r&#39;\begin{equation}&#39;)</span>
<span class="sd">    &#39;\\[&#39;</span>
<span class="sd">    &gt;&gt;&gt; pattern = fix(r&#39;\mbox\{(.+?)\}&#39;, application=&#39;match&#39;)</span>
<span class="sd">    &gt;&gt;&gt; replacement = fix(r&#39;\fbox{\g&lt;1&gt;}&#39;, application=&#39;replacement&#39;)</span>
<span class="sd">    &gt;&gt;&gt; re.sub(pattern, replacement, r&#39;\mbox{not}&#39;)</span>
<span class="sd">    &#39;\\fbox{not}&#39;</span>

<span class="sd">    Avoid mixing LaTeX commands and ordinary regular expression</span>
<span class="sd">    commands, e.g.:</span>

<span class="sd">    &gt;&gt;&gt; pattern = fix(r&#39;\mbox\{(\d+)\}&#39;, application=&#39;match&#39;)</span>
<span class="sd">    &gt;&gt;&gt; pattern</span>
<span class="sd">    &#39;\\\\mbox\\{(\\\\d+)\\}&#39;</span>
<span class="sd">    &gt;&gt;&gt; re.sub(pattern, replacement, r&#39;\mbox{987}&#39;)</span>
<span class="sd">    &#39;\\mbox{987}&#39;  # no substitution, no match</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">import</span> <span class="nn">string</span>
    <span class="n">problematic_letters</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="k">if</span> <span class="n">application</span> <span class="o">==</span> <span class="s">&#39;match&#39;</span> \
                          <span class="k">else</span> <span class="s">&#39;abfgnrtv&#39;</span>

    <span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="n">problematic_letters</span><span class="p">:</span>
        <span class="n">problematic_pattern</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span> <span class="o">+</span> <span class="n">letter</span>

        <span class="k">if</span> <span class="n">letter</span> <span class="o">==</span> <span class="s">&#39;g&#39;</span> <span class="ow">and</span> <span class="n">application</span> <span class="o">==</span> <span class="s">&#39;replacement&#39;</span><span class="p">:</span>
            <span class="c"># no extra \ for \g&lt;...&gt; in pattern</span>
            <span class="k">if</span> <span class="s">r&#39;\g&lt;&#39;</span> <span class="ow">in</span> <span class="n">pattern</span><span class="p">:</span>
                <span class="k">continue</span>

        <span class="n">ok_pattern</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\\\\</span><span class="s">&#39;</span> <span class="o">+</span> <span class="n">letter</span>
        <span class="k">if</span> <span class="n">problematic_pattern</span> <span class="ow">in</span> <span class="n">pattern</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ok_pattern</span> <span class="ow">in</span> <span class="n">pattern</span><span class="p">:</span>
            <span class="n">pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">problematic_pattern</span><span class="p">,</span> <span class="n">ok_pattern</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">pattern</span>


</div>
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">task</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="n">task</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>

    <span class="k">if</span> <span class="n">task</span> <span class="o">==</span> <span class="s">&#39;debugregex&#39;</span><span class="p">:</span>
        <span class="n">r</span> <span class="o">=</span> <span class="s">r&#39;&lt;(.*?)&gt;&#39;</span>
        <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&lt;r1&gt;is a tag&lt;/r1&gt; and &lt;s1&gt;s1&lt;/s1&gt; is too.&#39;</span>
        <span class="k">print</span> <span class="n">debugregex</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="n">s</span><span class="p">)</span>
        <span class="k">print</span> <span class="n">debugregex</span><span class="p">(</span><span class="s">r&#39;(\d+\.\d*)&#39;</span><span class="p">,</span><span class="s">&#39;a= 51.243 and b =1.45&#39;</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/scitools_logo.jpg" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../../np-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SciTools 0.8.3 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, H. P. Langtangen, J. Ring, ++.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
    </div>
  </body>
</html>